How to get element name with javascript?

I am trying to get the element name in Javascript. The value, if the element is <div /> , will be returned by "div" . If it is <img src="" /> , then "img" will be returned. I use jquery to select a bunch of elements and then call a custom function for all of them. As part of this function, I want to know what I'm dealing with. How to do it?

It seems to be easy. And I think I did it before, but I just can't find it. Google results continue to give me β€œget the item by name” no matter how I express it.

+4
source share
5 answers

Use nodeName (see this tagName note )

"My advice is not to use tagName at all. NodeName contains all the tagName functions and a few more. Therefore, nodeName is always the best choice.

+9
source
+1
source

Do you want element.nodeName Or, in jQuery:

 $(".includeMe").each(function(){ alert(this.nodeName); }); <img class="includeMe" src="puppies.jpg" /> <div class="includeMe">Hello World</div> <p class="includeMe">Don't forget me as well</p> 
0
source
 $(selector).each(function() { switch (this.tagName) { // Handle cases } }); 
0
source

For element.tagName and element.nodeName return the name of your tag in uppercase.

If you want to lowercase, just use element.tagName.toLowerCase() or element.nodeName.toLowerCase() .

0
source

All Articles