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

javascript - getting name value of clicked on class element jquery - Stack Overflow

programmeradmin2浏览0评论

I am currently trying the following but it keeps getting the first name value of the first element with the class "button-blue". How can I work correctly so that it gets the name value of the element clicked on with this class??

$(".button-blue").click(function() {

    alert($(".button-blue").attr('name'));

});

I am currently trying the following but it keeps getting the first name value of the first element with the class "button-blue". How can I work correctly so that it gets the name value of the element clicked on with this class??

$(".button-blue").click(function() {

    alert($(".button-blue").attr('name'));

});
Share Improve this question asked Aug 9, 2012 at 22:07 JakeJake 3,4867 gold badges41 silver badges60 bronze badges 1
  • You have to understand how Jq selectors work. When you use $(".someClass") you are getting all elements which have that class – Marcelo Assis Commented Aug 9, 2012 at 22:11
Add a comment  | 

2 Answers 2

Reset to default 20

Use this.name instead. Inside an event handler, this is the DOM element on which the event was triggered.

If you prefer to use .attr() or need a jQuery object containing that element for another reason, you can simply use $(this).attr('name')

Inside an event handler, this.tagName should give you the name.

发布评论

评论列表(0)

  1. 暂无评论