Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - $(this).attr('name') [closed]

Tags:

jquery

I have a name attribute assigned to a hyperlink.
When I do the following with jQuery link_name does not return anything.
Am I doing something wrong?

$("body").delegate("a", "click", function (event) {

    var link_name = $(this).attr('name');
    alert(link_name);
like image 979
Nate Pet Avatar asked Dec 28 '11 18:12

Nate Pet


People also ask

What does attr do in jQuery?

jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.

How to find attribute name in jQuery?

To get the name, you'd use $(selector). attr('name') which would return (in your example) 'xxxxx' .

How do you check if an element has an attribute in jQuery?

Using jQuery The idea is to use the . attr() method, which returns the attribute's value for an element if it is present and returns undefined if the attribute doesn't exist.


1 Answers

I'd use this (using newest jQuery):

$("body").on("click", "a", function (event) {
    var link_name = $(this).attr('name');
    alert(link_name);
});
like image 178
Rich Bradshaw Avatar answered Oct 11 '22 20:10

Rich Bradshaw