Faizan
Faizan

Reputation: 776

How to apply "id" in the following Code of Jquery

Dynamically created an element under the element having class "parent" and applied some css class to it i.e "child" now i want to apply some "id" to it so i want to know how to do it

$(".parent").append("<a class='child' href='#bookmark" + cAnchorCount++ + "'> "+ $(this).text()  +"</a>");

and how to bind some event to it like mouse over event to it

i tried this but didn't worked

$(".pushLinkBtnContainer").append("<a id='$(this).text()' class='pushLinkBtn' href='#bookmark" + cAnchorCount++ + "'>".bind("mouseover", function() {
                $(this).css("background-color", "red");
            }) + "</a>");

Upvotes: 1

Views: 56

Answers (3)

codingrose
codingrose

Reputation: 15699

Simple id:

$(".pushLinkBtnContainer").append("<a id='someid' class='pushLinkBtn' href='#bookmark" + cAnchorCount++ + "'> "+ $(this).text()  +"</a>");

Dynamic id:

1)

$(".pushLinkBtnContainer").append("<a id='someid"+variable_name+"' class='pushLinkBtn' href='#bookmark" + cAnchorCount++ + "'> "+ $(this).text()  +"</a>");

2)

$(".pushLinkBtnContainer").append("<a id='"+variable_name+"' class='pushLinkBtn' href='#bookmark" + cAnchorCount++ + "'> "+ $(this).text()  +"</a>");

Binding event:

$(".pushLinkBtnContainer").on("mouseover","#someid",function(){
    //execute statements
});

Upvotes: 2

Milind Anantwar
Milind Anantwar

Reputation: 82241

Use:

$(".pushLinkBtnContainer").append("<a id='" + cAnchorCount + '" class='pushLinkBtn' href='#bookmark" + cAnchorCount++ + "'> "+ $(this).text()  +"</a>");

Upvotes: 0

felipekm
felipekm

Reputation: 2910

$(".pushLinkBtnContainer").append("<a id="'pushLinkBtn_' + cAnchorCount + '" class='pushLinkBtn' href='#bookmark" + cAnchorCount++ + "'> "+ $(this).text()  +"</a>");

Upvotes: 0

Related Questions