1

I have selected some tag with jQuery:

$('select, :checkbox, :radio').each(function(){
   // ...
});

Now, I need to get the name of the current tag:

$('select, :checkbox, :radio').each(function(){
   var tag_name = $(this). ???
   alert(tag_name);
});

Expected result: "select", "input" and so on.

So, I need to know, how to get the tag name of element. Maybe without jQuery, with native javascript functions - no matter how.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ax.
  • 320
  • 3
  • 4
  • 12
  • Thanks a lot to all! There a lot of very usefull information in all your answers. – Ax. Jul 26 '11 at 19:47
  • Also check: http://stackoverflow.com/questions/5347357/jquery-get-selected-element-tag-name/9538913#9538913 – Chepech Mar 09 '12 at 22:39

6 Answers6

7

You can use the HTML DOM native tagName property. Try this:

var tag_name = this.tagName;
Chandu
  • 81,493
  • 19
  • 133
  • 134
4
$('select, :checkbox, :radio').each(function(){
   var tag_name = this.tagName;
   alert(tag_name);
});
Shef
  • 44,808
  • 15
  • 79
  • 90
1

Just this.tagName will give you the node name.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

Try this:

$('select, :checkbox, :radio').each(function(){
   alert($(this).get(0).nodeName);
});
Alex Heyd
  • 1,323
  • 1
  • 10
  • 17
  • this.tagName might be better actually... http://stackoverflow.com/questions/4878484/different-between-tagname-and-nodename-jquery – Alex Heyd Jul 26 '11 at 19:40
1

sure... very simple

Here is a working example

http://jsfiddle.net/L96KG/

here is the reference source

Can jQuery provide the tag name?

Community
  • 1
  • 1
samccone
  • 10,746
  • 7
  • 43
  • 50
1

You can also do:

$('select, :checkbox, :radio').each(function(el){
    alert(el.tagName);
});
citizen conn
  • 15,300
  • 3
  • 58
  • 80