0

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?

3
  • usually its the opposite you want <a href="#"><img src=""></a> Commented Jan 4, 2014 at 20:41
  • If you want double quotes in a double-quoted string, you need to escape them. I'm sure your console must show you some error messages. Also, consider attaching the node you create somewhere in the DOM. Commented Jan 4, 2014 at 20:41
  • no i want it exactly like i have written . Commented Jan 4, 2014 at 20:45

1 Answer 1

1

Just do:

$("<img>").addClass("myclass").after("<a href='#'></a>").appendTo("body");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.