javascript - Fetching data from many input fields using getElementsByTagName() method -


<!doctype html> <html> <head> <script> function getelements() {   var v = document.getelementbyid('view');   var x=document.getelementsbytagname("input");   for(var i=0; i<x.length; i++){   v.innerhtml = x[i].value+"<br>";   }   } </script> </head> <body>  <input type="text" size="20"><br> <input type="text" size="20"><br> <input type="text" size="20"><br><br> <input type="button" onclick="getelements()" value="how many input elements?">  <p id='view'></p> </body> </html> 

this code want fetch values of fields , iterate them below in "p" tag kept showing me value of last input submit value.

the nature of program me fetch data many inputs elements including file, upload field , submit them server script.

you need change html , javascript following not give button's value(which not needed) :

html:

<input type="text" size="20"><br> <input type="text" size="20"><br> <input type="text" size="20"><br><br>     <button onclick="getelements()" value="how many input elements?" >how many input elements?</button> <p id='view'></p> 

js:

function getelements() {   var v = document.getelementbyid('view');     v.innerhtml = "";   var x=document.getelementsbytagname("input");   for(var i=0; i<x.length; i++){       v.innerhtml += x[i].value+"<br>";   }   } 

demo : http://jsfiddle.net/f7blq0b4/


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -