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?
3 Answers
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!
Comments
Try this:
els = $('#' + elements + ' *').map(function(){
return this.nodeName;
}).get();
console.log(els);