Get element's tagname using DOM's tagName property

9/3/2013


A couple of weeks ago I posted how can you get an element's tagname using jQuery's .prop() method. A simpler alternative could be the below code:

$(el).get(0).tagName.toLowerCase();

// example
$("#someElement").get(0).tagName.toLowerCase();

The .get() method (don't confuse this method with the jQuery's AJAX .get(), they are completely different) retrieves the DOM element and then we simply get it's tagName property (and convert it to lowercase). We can even retrieve the DOM element from a jQuery array using the below syntax (without the .get() method) and have the exact same result.

$(el)[0].tagName.toLowerCase();

// example
$("#someElement")[0].tagName.toLowerCase();

I believe that the above method is easier and faster than the previous one.