最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access custom attributes via jquery - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

Reference 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")

发布评论

评论列表(0)

  1. 暂无评论