I have a list of elements which I got els = $('#' + elements + ' *').get();. Now I want to get tagNames of these elements and put them in separate array. How can I get tag name from els?
Asked
Active
Viewed 1,082 times
0
Om3ga
- 30,465
- 43
- 141
- 221
3 Answers
1
You can use .tagName, which is a property of HTMLElement:
var tagNames = []; // Array of tag names
for(var i = 0; i < els.length; i++){
tagNames.push(els[i].tagName);
}
tagName gives you the tag name in caps. You can convert it to small case by using .toLowerCase() on .tagName to get lower cased tag names.
Note :
As an alternative to tagName, you could also use nodeName. For tags, values of tagName and nodeName are identical!
John Slegers
- 45,213
- 22
- 199
- 169
Amit Joki
- 58,320
- 7
- 77
- 95
1
Try this:
els = $('#' + elements + ' *').map(function(){
return this.nodeName;
}).get();
console.log(els);
Demo @ Fiddle
John Slegers
- 45,213
- 22
- 199
- 169
Jai
- 74,255
- 12
- 74
- 103
0
No need to use .get() method. just $(selector)[0].nodeName like this:
var tags = [];
for (i in els){
tags.push(els[i].nodeName);
}
MasqueradeCircus
- 834
- 6
- 8