-2

I have a php script where i have a textbox which is placed inside a while loop, So the textbox Id varies as follows.

while{$i<10)
{
<input type = "text" id='text$i' />
$i++;
} 

Now in javascript i want to retrieve the value of that textbox

 <script>
  var id=document.getElementById("textbox//Whatshouldbe(i)value").value();
 </script>

If $i=9 then the textbox Id will be textbox9. How to get the id name in javacript?

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
NewBie
  • 67
  • 3
  • 11

3 Answers3

1

Add uniq class to get all your inputs sin future

while{$i<10)
{
<input type = "text" class="myClass" id='text$i' />
$i++;
} 

Now in javascript retrieve the id and value of that textboxes

 <script>
  var elems = document.querySelectorAll(".myClass");
  for (var i = 0; i < elems.length; i++) {
    console.log (elems[i].id);
    console.log (elems[i].value);
  }
 </script>
qiAlex
  • 4,290
  • 2
  • 19
  • 35
0

You can use getElementsByTagName function, it returns a HTMLCollection of elements with the given tag name.

var elements = document.getElementsByTagName("input")
for (var i = 0; i < elements.length; i++) {
    values =elements[i].value;
    alert(values);    
    //Do something
}
0

document.querySelectorAll('[id^="text"]')

This should find all element with ID starting with "text". After that iterate through them and check if they are .checked to find value of checked ones

Anarion
  • 1,048
  • 6
  • 16