0

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?

0

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

els = $('#' + elements + ' *').map(function(){
    return this.nodeName;
}).get();

console.log(els);

Demo @ Fiddle

Comments

0

No need to use .get() method. just $(selector)[0].nodeName like this:

var tags = [];
for (i in els){
    tags.push(els[i].nodeName);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.