jquery - Assign value for multiple input boxes using javascript? -


hi have problem in assign single values multiple input boxes. trying many ways assign 1 text box. how can assign multiple text boxes.

note: have same id input boxes.

my code given below

<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>untitled document</title> <script type="text/javascript"> function getinputs() {   var inputs = document.getelementsbytagname('input');   var ids = new array();   for(var = 0; < inputs.length; i++)   {     if(inputs[i].getattribute('id').tolowercase()== 'myid')     {               document.getelementsbyid('myid').value="1";     }   }    }     window.onload = getinputs; </script> </head> <body> <form>   <input type="text"  id="myid"><br>   <input type="text"  id="myid"><br>   <input type="text"  id="myid"><br>   <input type="text"  id="myid"><br> </form> </body> </html> 

can help?

it assigns value 1 of because id's should unique; therefore you're going end targetting first 1 value assignment.

change html use class instead:

<input type="text"  class="myids"><br> <input type="text"  class="myids"><br> <input type="text"  class="myids"><br> <input type="text"  class="myids"><br> 

then, can adapt javascript accordingly.


jquery

in jquery, set value using:

$('.myids').val('value of them here');

jquery jsfiddle here.


pure javascript

in javascript, you'd use getelementsbyclassname() , iterate through them, giving them same value.

var x = document.getelementsbyclassname('myids'); for(i = 0; < x.length; i++) {   x[i].value = "new!"; } 

pure javascript jsfiddle here.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -