Kabir Khan Kabir Khan - 2 years ago 78
Javascript Question

How can I Add a tag in a div

I want to add a a tag in

frontpage-course-list div
but, the below code will add class in
frontpage-course-list div

$( "#frontpage-course-list" ).add( "a" ).addClass( "myclass" );
var pdiv = $( "#frontpage-course-list" ).add( "a" );

Please let me know how to add

Answer Source

You have to use append:

 $( "#frontpage-course-list" ).append("<a class='myclass'>My custom link</a>");

If you want an img inside a you can do

$( "#frontpage-course-list" ).append("<a class='myclass'><img src='/path/to/image'></a>");

Or if you want an image (not a link) you can write the following:

$( "#frontpage-course-list" ).append("<img src='/path/to/image'>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download