Reputation: 67
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
Upvotes: 2
Views: 73
Reputation: 3838
The following will not save the added elements, because the .add() method creates a new set and leaves the original set in pdiv
unchanged:
$( "#frontpage-course-list" ).add( "a" ).addClass( "myclass" );
var pdiv = $( "#frontpage-course-list" ).add( "a" );// WRONG, pdiv will not change.
To over come this, use .append
.
$("#frontpage-course-list").append("<a class='myclass'></a>");
Upvotes: 0
Reputation: 1014
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'>");
Upvotes: 1
Reputation: 25527
Use append
$("#frontpage-course-list").append("<a class='yourclass'></a>");
This will add the anchor as last child of that specified element(frontpage-course-list
).
If you want to add the anchor as firt child, then use prepend
$("#frontpage-course-list").prepend("<a class='yourclass'></a>");
You can also try something like,
var anchor = $("<a/>", {
'class': 'yourclass',
'href': 'your href'
});
$("#frontpage-course-list").append(anchor);
Upvotes: 2
Reputation: 388316
If you want to create a new anchor element with the said class then add it to the div then you can use
$('<a />', {
'class': 'myclass'
}).appendTo('#frontpage-course-list')
The .add() will return a jQuery object which contain the elements in the calling set and the elements returned by the passing selector.
Upvotes: 0