Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 229
kamikaze_pilot Avatar asked Jun 14 '11 11:06

kamikaze_pilot


2 Answers

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

like image 134
venimus Avatar answered Oct 01 '22 13:10

venimus


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.

like image 22
James Westgate Avatar answered Oct 01 '22 13:10

James Westgate