jQuery – How to get the tag name
Published: May 6, 2010 , Updated: May 24, 2010 , Author: mkyong
To get the element tag name, you can use the tagName function. There are two ways to use it :
1) .get(0).tagName
Select an element that has a class name of “classTag1″, and use the .get(0).tagName function to display its tag name.
$('.classTag1').get(0).tagName;2) .[0].tagName
2. Select an element that has a class name of “classTag1″, and use the .[0].tagName function to display its tag name.
$('.classTag1')[0].tagName;Example
<html> <head> <title>jQuery Get Tag Name</title> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> </head> <script type="text/javascript"> $(document).ready(function(){ var $tag = $('p')[0].tagName; //'P' alert($tag); var $tag = $('.classTag1')[0].tagName; //'DIV' alert($tag); var $tag = $('#idTag1')[0].tagName; //'DIV' alert($tag); var $tag = $('p').get(0).tagName; //'P' alert($tag); var $tag = $('.classTag1').get(0).tagName; //'DIV' alert($tag); var $tag = $('#idTag1').get(0).tagName; //'DIV' alert($tag); }); </script> <body> <h1>jQuery Get Tag Name</h1> <p> This is paragrah 1 </p> <div class="classTag1"> This is class='classTag1' </div> <div id="idTag1"> This is id='idTag1' </div> </body> </html>
Note : You can find more similar articles at - jQuery Tutorials








working for me. Thanks for sharing.
[...] jQuery. Getting the node name is a little more difficult but a method for getting it is provided here. From a quick look, I’m assuming you’d want to do something along the lines [...]
Also, tagName is not a function, but an attribute. Upon reading “use the .get(0).tagName function”, I used automatically typed “.get(0).tagName();” in my code (with the parentheses after tagName). To avoid confusion, it would be better to change “function” in “attribute”.
Doesn’t work !
You can access simply as it:
$(element).attr(“tagName”)
that doesn’t works as good as the other solution (using jscript on the dom element directly)
Hi there,
this has nothing to do with jQuery – ‘tagName’ as a function does exist neither in jQuery nor in JavaScript. ‘tagName’ is a property of every DOM-Element – so you can use ist like ‘document.getElementById(“myId”).tagName’. The only jQuerything in your post is how you retrieve the DOM-Element: $(‘myselector’).get(0) and $(‘myselector’)[0] both just return the first DOM-Element which matches ‘myselector’ – after that, you are leaving the jQuery-Scope and are just accessing the DOM-element-property ‘tagName’
Hi Ingmar,
Thanks for sharing your thoughts.