Kabir Khan Kabir Khan - 7 months ago 4
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

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'>");
Comments