HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How can I get the data-id attribute?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
datahowattributethecanget

Problem

I'm using the jQuery Quicksand plugin. I need to get the data-id of the clicked item and pass it to a webservice.

How do I get the data-id attribute? I'm using the .on() method to re-bind the click event for sorted items.



$("#list li").on('click', function() {
// ret = DetailsView.GetProject($(this).attr("#data-id"), OnComplete, OnTimeOut, OnError);
alert($(this).attr("#data-id"));
});








Solution

To get the contents of the attribute data-id (like in link) you have to use

$(this).attr("data-id") // will return the string "123"


or .data() (if you use newer jQuery >= 1.4.3)

$(this).data("id") // will return the number 123


and the part after data- must be lowercase, e.g. data-idNum will not work, but data-idnum will.

Code Snippets

$(this).attr("data-id") // will return the string "123"
$(this).data("id") // will return the number 123

Context

Stack Overflow Q#5309926, score: 2049

Revisions (0)

No revisions yet.