I need to access to the custom attribute or data of my link but I can't. My code is simple yet in repeater. I don't know if this is causing a problem. Here is the code:
<a class="showAllComm" data-userid='<%# DataBinder.Eval(Container.DataItem, "USER_ID")%>' href="#sa">Show all ments</a>
Here is my click event:
$('.showAllComm').click(function(index, element) {
var mId = $(element).data("userid");
})
mId is undefined but I can see it in the source code that it has value of 1.
how can I access to the userId?
Thank you
I need to access to the custom attribute or data of my link but I can't. My code is simple yet in repeater. I don't know if this is causing a problem. Here is the code:
<a class="showAllComm" data-userid='<%# DataBinder.Eval(Container.DataItem, "USER_ID")%>' href="#sa">Show all ments</a>
Here is my click event:
$('.showAllComm').click(function(index, element) {
var mId = $(element).data("userid");
})
mId is undefined but I can see it in the source code that it has value of 1.
how can I access to the userId?
Thank you
Share Improve this question asked Sep 14, 2011 at 22:27 PabucPabuc 5,6388 gold badges39 silver badges52 bronze badges2 Answers
Reset to default 8Reference the element with this
instead of the second parameter:
var mId = $(this).data("userid");
The arguments passed to an event handler are not the index
and element
as you'd have in .each()
.
By default, you just get a single event
argument passed.
DEMO: http://jsfiddle/Jjbwd/
$('.showAllComm').click(function( event ) {
alert( event.type ) // click
var mId = $(this).data("userid");
});
The data
method is not a shortcut for the attr
method. It takes an element and an attribute, per the docs
Just use attr("data-userid")