1
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,250
  • 8
  • 20
CharlesX
  • 378
  • 3
  • 6
  • 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

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,589
  • 16
  • 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
1

Use document.getElementsByTagName('input') instead.

It'll return you an array with every input.

zessx
  • 66,778
  • 28
  • 130
  • 153
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,536
  • 1
  • 18
  • 31
1

You can use:

document.getElementsByTagName("input");

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

Rami
  • 7,027
  • 1
  • 21
  • 19