jQuery: Get selected element tag name

JavascriptHtmlJqueryCssHtmlelements

Javascript Problem Overview


Is there an easy way to get a tag name?

For example, if I am given $('a') into a function, I want to get 'a'.

Javascript Solutions


Solution 1 - Javascript

You can call .prop("tagName"). Examples:

jQuery("<a>").prop("tagName"); //==> "A"
jQuery("<h1>").prop("tagName"); //==> "H1"
jQuery("<coolTagName999>").prop("tagName"); //==> "COOLTAGNAME999"


If writing out .prop("tagName") is tedious, you can create a custom function like so:

jQuery.fn.tagName = function() {
  return this.prop("tagName");
};

Examples:

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.fn.tagNameLowerCase = function() {
  return this.prop("tagName").toLowerCase();
};

Examples:

jQuery("<a>").tagNameLowerCase(); //==> "a"
jQuery("<h1>").tagNameLowerCase(); //==> "h1"
jQuery("<coolTagName999>").tagNameLowerCase(); //==> "cooltagname999"

Solution 2 - Javascript

You can use the DOM's nodeName property:

$(...)[0].nodeName

Solution 3 - Javascript

As of jQuery 1.6 you should now call prop:

$target.prop("tagName")

See http://api.jquery.com/prop/

Solution 4 - Javascript

jQuery 1.6+

jQuery('selector').prop("tagName").toLowerCase()

Older versions

jQuery('selector').attr("tagName").toLowerCase()

toLowerCase() is not mandatory.

Solution 5 - Javascript

This is yet another way:

$('selector')[0].tagName

Solution 6 - Javascript

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 :

jQuery('selector')[0].tagName.toLowerCase()

Option 2

jQuery('selector')[0].nodeName.toLowerCase()

Solution 7 - Javascript

nodeName will give you the tag name in uppercase, while localName will give you the lower case.

$("yourelement")[0].localName 

will give you : yourelement instead of YOURELEMENT

Solution 8 - Javascript

get clicked tag name with jQuery

jQuery("YOUR_SELECTOR").click(function (e) {
     console.log(jQuery(e.target).prop("tagName").toLowerCase())
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionconfiguratorView Question on Stackoverflow
Solution 1 - JavascripttilleryjView Answer on Stackoverflow
Solution 2 - JavascriptSLaksView Answer on Stackoverflow
Solution 3 - JavascriptRobView Answer on Stackoverflow
Solution 4 - JavascriptDayron GallardoView Answer on Stackoverflow
Solution 5 - JavascriptChepechView Answer on Stackoverflow
Solution 6 - JavascriptJohn SlegersView Answer on Stackoverflow
Solution 7 - JavascriptDonovan PView Answer on Stackoverflow
Solution 8 - JavascriptMahdi BashirpourView Answer on Stackoverflow