kamikaze_pilot
kamikaze_pilot

Reputation: 14834

get the tag name of object returned by getElementsByName

so suppose I call document.getElementsByName("title"); in javascript

and I want to know the type of the tag of the element that is returned by that function, for instance, to see if it's a meta tag or a div tag or a span tag, etc

how would I go about doing this?

Upvotes: 11

Views: 27226

Answers (3)

James Westgate
James Westgate

Reputation: 11444

You have returned a NodeList object, so you would need to be more specific with your selector, or choose the first element using an index of 0, as in the other answers.

Whilst you can use nodeName or tagName, nodeName is the better option.

Upvotes: 5

James Allardice
James Allardice

Reputation: 165971

You can use the tagName property like so:

document.getElementsByName('name')[index].tagName;

You need the index as the getElementsByName function returns an array.

Upvotes: 1

venimus
venimus

Reputation: 6047

document.getElementsByName("title"); returns a set of elements not a single element so within a cycle you could use element.tagName to get the tag

basicly

document.getElementsByName("title")[0].tagName should work

Upvotes: 15

Related Questions