1

In fact, I am working on a small PHP script.On a page the PHP generates code like this :

<div id="ya">sjaka</div>
<p id="ya"><input id="color" type="text" value="text1"></input></p>
<span id="ya"><input id="color" type="text" value="text2"></input></span>

What i want is that to get the value of the input inside the span with the id ya.for that reason i am using this js code :

x = document.getElementById("ya");
 color = x.querySelector("#color").value;

But it doesn't seem to work it returns the result inside the P tag.How can I achieve this ?

Naftali
  • 144,921
  • 39
  • 244
  • 303
Belatar Yassine
  • 153
  • 2
  • 10

1 Answers1

6

You should not have multiple elements on the page with the same ID.

ids should be unique.

Consider using a class instead.

Otherwise JavaScript just chooses one of them to select.

Naftali
  • 144,921
  • 39
  • 244
  • 303