Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of an element with Javascript?

Tags:

javascript

dom

I'm trying to get the name of an element in Javascript. Meaning if the element is <div />, then "div" would be returned. If it's <img src="" /> then "img" would be returned. I'm using jquery to select a bunch of elements and then calling a custom function on all of them. Within that function I want to know what I'm dealing with. How do I do this?

Seems like a simple thing. And I think I've done it before but I just can't find it. Google results keep giving me "get element by name" no matter how I phrase it.

like image 826
fent Avatar asked Dec 30 '22 06:12

fent


1 Answers

Use nodeName (see this note about tagName):

"My advice is not to use tagName at all. nodeName contains all functionalities of tagName, plus a few more. Therefore nodeName is always the better choice."

like image 180
Annie Avatar answered Jan 14 '23 10:01

Annie