1.<input id="kw1"></input>
2.<input></input>
The first one, I can use document.getElementById
to get the object, but on some websites, like the second, there is no id in element, but I also want to get the object. How can I do this ? And do not use JQuery
1.<input id="kw1"></input>
2.<input></input>
The first one, I can use document.getElementById
to get the object, but on some websites, like the second, there is no id in element, but I also want to get the object. How can I do this ? And do not use JQuery
-
document.getElementsByTagName('input')
gives array of input elements. – Mr_Green Commented Jul 4, 2014 at 8:05 -
If you want to match that specific element, you will need something to distinguish it from the others. Either a matchable ancestor element (by id or class, for instance), or the index of that
<input>
element relative to the other elements of the same type. – Frédéric Hamidi Commented Jul 4, 2014 at 8:08 - possible duplicate of How can I get query string values in JavaScript? – CharlesX Commented Sep 11, 2014 at 13:14
- How strange that the DOM was designed with no inherent identification, no serial number, for each node. Was this to save a little space? Nodes have forward and backward links, so saving space was not a primary concern. Was it, then, an oversight? – David Spector Commented Aug 20, 2023 at 17:08
4 Answers
Reset to default 9you can do this by using :
getElementsByTagName
example :
var elements = document.getElementsByTagName('input'); // returns array
for more broader details in using it click here
Use document.getElementsByTagName('input')
instead.
It'll return you an array with every input
.
You can use document.getElementsByTagName('input'), but this give you all input elements.
But since you have nothing else to identify that element, you can not be more specific.
You can use:
document.getElementsByTagName("input");
which will return a list of objects of type "input"