3

How can I find the el tag name below (eg. div, p or span) ?

$('div, p, span').each(function(){ 
  var el = $(this);



});

4 Answers4

6

Use the tagName property:

$('div, p, span').each(function(){ 
  var tag = this.tagName;
});

Note: tagName is a property of the element, not a jQuery method.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

Using nodeName is better than tagName if you consider consistency between browsers. This has good explanation: Difference between .tagName and .nodeName

$('div, p, span').each(function(){ 
  var el = this.nodeName;
});
Community
  • 1
  • 1
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
  • 1
    There won't be any other type of nodes than tags in a collection created from that selector. Actually, there will never be any other type of nodes in a jQuery collection created from *any* selector. – Guffa Jul 14 '11 at 11:27
0

You can use

$(this).prop('tagName')

or

$(e.target).prop('tagName')

to get the selected tag name.

Kris
  • 35
  • 2
  • 8
0
<script>
$(function(){
    $("p,div").bind('click',function(){
       var el = this.tagName;
       alert(el);
    });
});
</Script>
<p>p</p>
<div>div</div>

....

more info (example) here

genesis
  • 50,477
  • 20
  • 96
  • 125
  • That doesn't work, there is no tagName property in the jQuery object. (I even tried it, to be 100% sure.) – Guffa Jul 14 '11 at 11:07
  • yes, sorry. It will work now. working demo: http://sandbox.phpcode.eu/g/e26a7.php – genesis Jul 14 '11 at 11:10