You should NOT use jQuery('selector').attr("tagName").toLowerCase(), because it only works in older versions of Jquery.
You could use $('selector').prop("tagName").toLowerCase() if you're certain that you're using a version of jQuery thats >= version 1.6.
Note :
You may think that EVERYONE is using jQuery 1.10+ or something by now (January 2016), but unfortunately that isn't really the case. For example, many people today are still using Drupal 7, and every official release of Drupal 7 to this day includes jQuery 1.4.4 by default.
So if do not know for certain if your project will be using jQuery 1.6+, consider using one of the options that work for ALL versions of jQuery :
jQuery("<a>").tagName(); //==> "A"
jQuery("<h1>").tagName(); //==> "H1"
jQuery("<coolTagName999>").tagName(); //==> "COOLTAGNAME999"
Note that tag names are, by convention, returned CAPITALIZED. If you want the returned tag name to be all lowercase, you can edit the custom function like so:
jQuery 1.6+
Older versions
toLowerCase() is not mandatory.
You should NOT use
jQuery('selector').attr("tagName").toLowerCase()
, because it only works in older versions of Jquery.You could use
$('selector').prop("tagName").toLowerCase()
if you're certain that you're using a version of jQuery thats >= version 1.6.Note :
You may think that EVERYONE is using jQuery 1.10+ or something by now (January 2016), but unfortunately that isn't really the case. For example, many people today are still using Drupal 7, and every official release of Drupal 7 to this day includes jQuery 1.4.4 by default.
So if do not know for certain if your project will be using jQuery 1.6+, consider using one of the options that work for ALL versions of jQuery :
Option 1 :
Option 2
You can use the DOM's
nodeName
property:As of jQuery 1.6 you should now call prop:
See http://api.jquery.com/prop/
You can call
.prop("tagName")
. Examples:If writing out
.prop("tagName")
is tedious, you can create a custom function like so:Examples:
Note that tag names are, by convention, returned CAPITALIZED. If you want the returned tag name to be all lowercase, you can edit the custom function like so:
Examples:
This is yet another way: