user979390 user979390 - 16 days ago 5
HTML Question

How to create link element with image as anchor with jquery?

I know how to create elements with jquery using something like:

$('<div/>').appendTo('body');


How can I create this:

<a href=""><img src="" /></a>


Using the same technique?

Answer
$('<img />').attr({
  src:'some image url',
  width:'width in intiger',
  height:'integer'
}).appendTo($('<a />').attr({
  href:'somelink'
}).appendTo($('#someElement')));
Comments