2
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

Blisskarthik
  • 1,246
  • 8
  • 20
CharlesX
  • 418
  • 4
  • 7
  • 14
  • `document.getElementsByTagName('input')` gives array of input elements. – Mr_Green Jul 04 '14 at 08: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 `` element relative to the other elements of the same type. – Frédéric Hamidi Jul 04 '14 at 08:08
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – CharlesX Sep 11 '14 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 Aug 20 '23 at 17:08

4 Answers4

8

you can do this by using :

getElementsByTagName

example :

var elements = document.getElementsByTagName('input'); // returns array

for more broader details in using it click here

Yaje
  • 2,753
  • 18
  • 32
  • if he will not dynamically generate any element, `document.querySelectorAll('input')` is considerable. – Yaje Jul 04 '14 at 08:08
  • `querySelectorAll()` can match dynamically-added elements. – Frédéric Hamidi Jul 04 '14 at 08:09
  • [MDN](https://developer.mozilla.org/en/docs/Web/API/Document.querySelectorAll) says differently. or did i get it wrong? – Yaje Jul 04 '14 at 08:10
  • 3
    Are you referring to the "non-live nodelist"? That means the returned node list will not automatically reflect further changes in the DOM. However, all the matching elements at the time of the call are returned. – Frédéric Hamidi Jul 04 '14 at 08:18
2

Use document.getElementsByTagName('input') instead.

It'll return you an array with every input.

zessx
  • 68,042
  • 28
  • 135
  • 158
1

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.

Jeroen1984
  • 1,616
  • 1
  • 19
  • 32
1

You can use:

document.getElementsByTagName("input");

which will return a list of objects of type "input"

Rami
  • 7,162
  • 1
  • 22
  • 19