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>
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts