JQuery - $ (this) .attr ('name')

I have an attribute nameassigned to a hyperlink.
When I do the following: jQuery link_namereturns nothing.
Am I doing something wrong?

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

    var link_name = $(this).attr('name');
    alert(link_name);
+5
source share
2 answers

I would use this (using the latest jQuery):

$("body").on("click", "a", function (event) {
    var link_name = $(this).attr('name');
    alert(link_name);
});
+18
source

Quote OP:

Am I doing something wrong?

As others have noted, your code should work if you add missing closing brackets. });

You also did not specify which version of jQuery, however, using the latest version 1.7, you should use 1 instead and instead .on() delegate()prop()attr()

$("body").on("click", "a", function (event) {
    var link_name = $(this).prop('name');
    alert(link_name);
});
  • Starting with jQuery 1.7, .delegate () has been replaced with the .on () method.
+2
source

All Articles