Adeel
Adeel

Reputation: 19238

how to get data attributes of dynamically generated element

while throwing dynamic content (through AJAX) from server, i have following response.

<a data-contentid="1">Some content</a> ...

i have binded the click event using live. The problem is in my click event, i am unable to get data attributes, so manually attaching is not an option for my case. any ideas?

Example code:

 $('a.delContent').live("click",function () {

            var cid= jQuery.data(this, "contentid");

            alert(cid);  //undefined
            return false;
        }
           );

Upvotes: 2

Views: 6448

Answers (2)

DarthJDG
DarthJDG

Reputation: 16591

Try this inside your click event handler:

$(this).data('contentId')

Works in jsfiddle: http://jsfiddle.net/Mk2zy/

Upvotes: 3

Vivek
Vivek

Reputation: 11028

try this....

var attrValue = $(serverresponse).find('<a>').attr('data-contentId');
alert(attrValue );

Upvotes: 1

Related Questions