admin管理员组

文章数量:1394977

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.

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.

Share Improve this question edited Feb 4, 2018 at 11:24 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Jul 26, 2011 at 19:35 Ax.Ax. 3203 gold badges4 silver badges12 bronze badges 2
  • Thanks a lot to all! There a lot of very usefull information in all your answers. – Ax. Commented Jul 26, 2011 at 19:47
  • Also check: stackoverflow./questions/5347357/… – Chepech Commented Mar 9, 2012 at 22:39
Add a ment  | 

6 Answers 6

Reset to default 7

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

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

Just this.tagName will give you the node name.

Try this:

$('select, :checkbox, :radio').each(function(){
   alert($(this).get(0).nodeName);
});

sure... very simple

Here is a working example

http://jsfiddle/L96KG/

here is the reference source

Can jQuery provide the tag name?

You can also do:

$('select, :checkbox, :radio').each(function(el){
    alert(el.tagName);
});

本文标签: javascriptHow to get the name of the current selected HTML tagStack Overflow