I\'m trying to get the name of an element in Javascript. Meaning if the element is , then
\"div\"
would be returned. If it\'s &l
tagName or nodeName
You want element.nodeName
Or, within 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>
$(selector).each(function() {
switch (this.tagName) {
// Handle cases
}
});
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."
For element.tagName
and element.nodeName
return the name of your tag, in uppercase.
If you want it in lowercase, just use element.tagName.toLowerCase()
or element.nodeName.toLowerCase()
.