user3149456
user3149456

Reputation: 7

adding an anchor tag with image in javascript

I have created an image tag in JQuery. Now I need to add one more anchor tag with it. How can I implement it with JavaScript?

I tried this:

var imgg = document.createElement("img");
imgg.className='myclass';
$( ".myclass" ).add( "<a href="#">Test</a>" );   

but it isn't working. It should appear as:

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

Which function should I use to add one more anchor tag within img tag?

Upvotes: 0

Views: 401

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Just do:

$("<img>").addClass("myclass").after("<a href='#'></a>").appendTo("body");

Upvotes: 1

Related Questions