How to get the tag name in jQuery ?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
How to get the tag name in jQuery

Hi Dev’s, Now today in this example,I will show you how to get the tag name by using  jquery. So you can select to class or id that by using the function then get to tab name . Now you can simply and easy to get tab name by using jquery.

Solution 1 :- .get(0).tagName

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

$('.class').get(0).tagName; 
Solution 2 :- .[0].tagName

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

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

Example

<html>
<head>
<title>jQuery Get Tag Name - CodingsPoint.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 - CodingPoint.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>

Read Also : How Laravel Livewire Click Event work?

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →