Jake
Jake

Reputation: 3486

getting name value of clicked on class element jquery

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'));

});

Upvotes: 4

Views: 20926

Answers (2)

Berend de Boer
Berend de Boer

Reputation: 2112

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

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318508

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')

Upvotes: 20

Related Questions