Skip to content

All topics  /  jQuery

How to get the tag name in jQuery

jQuery ·

Hi Guys,

In this example,I will learn you how to get the tag name in jquery.you can select to class or id that using the function then get to tab name . you can simply and easy to get tab name using jquery.

Solution 1 :- .get(0).tagName


Select an element that has a class name of “class”, and use the .get(0).tagName function to get this tag name.

$('.class').get(0).tagName; 

Solution 2 :- .[0].tagName

Select an element that has a class name of “class”, and use the .[0].tagName function to get this tag name.

$('.class')[0].tagName;

Example

<html>
<head>
<title>jQuery Get Tag Name - nicesnippets.com</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 = $('.class')[0].tagName; //'DIV'
    alert($tag);

	
    var $tag = $('#id')[0].tagName; //'DIV'
    alert($tag);

	
    var $tag = $('p').get(0).tagName; //'P'
    alert($tag);

	
    var $tag = $('.class').get(0).tagName; //'DIV'
    alert($tag);

	
    var $tag = $('#id').get(0).tagName; //'DIV'
    alert($tag);	
});
</script>
<body>
<h1>jQuery Get Tag Name - nicesnippets.com/</h1>
    <p>
        This is paragrah tag
    </p>

	
    <div class="class">
        This is class='class'
    </div>

	
    <div id="id">
        This is id='id'
    </div>
</body>
</html>

It will help you....