html - Javascript: Input bar isn't working or giving any result -
i made program asks user how many input bars wants display , user has type in values input bars , click verify issue having whenever click on verify, nothing happens. error:
uncaught typeerror: cannot read property 'value' of null
whenever click on verify.
here codes:
<html> <head> <script language="javascript"> var x = []; var choose; var i; var xar=[]; choose=parsefloat(prompt("how many inputs u want display")); for(i=0;i<choose;i++) x[i]=document.getelementbyid("cont"+i).style.visibility="visible"; function hola() { for(i=0;i<choose;i++) document.getelementbyid("cont"+i).style.visibility="visible"; } function verify(){ for(i=0;i<choose;i++){ xar[i]=document.getelementbyid("usernumber"+i).value; } for(i=0;i<choose;i++) if(xar[i]==i) alert("good"); else alert("wrong"); } </script> </head> <body> <input type="button" onclick="hola()" value="hidefield0" id="boton0"> <div id="cont0" style="visibility: hidden;"> <input type="text" id="usernumber" class="something"> </div> <input type="button" onclick="hola()" value="hidefield2" id="boton1"> <div id="cont1" style="visibility: hidden;" class="something"> <input type="text" id="username2"> </div> <input type="button" onclick="hola()" value="hidefield1" id="boton2"> <div id="cont2" style="visibility: hidden;" class="something"> <input type="text" id="username3"> </div> <input type="button" onclick="hola()" value="hidefield3" id="boton3"> <div id="cont3" style="visibility: hidden;" class="something"> <input type="text" id="username4"> </div> <input type="button" onclick="hola()" value="hidefield4" id="boton4"> <div id="cont4" style="visibility: hidden;" class="something"> <input type="text" id="username5"> </div> <input type="button" onclick="hola()" value="hidefield5" id="boton5"> <div id="cont5" style="visibility: hidden;" class="something"> <input type="text" id="username6"> </div> <input type="button" id="verifybtn" value="verify" onclick="verify()"> </body> <html>
the script failing when reaches portion:
document.getelementbyid("usernumber"+i)
this indicates there no element called usernumber + i
(i.e. "usernumber3") call .value
property of (cannot read property 'value' of null) .
and that's because have elements ids of "username2, username3, username4, etc." , "usernumber", not "usernumber" plus number.
var x = []; var choose; var i; var xar=[]; choose=parsefloat(prompt("how many inputs u want display")); for(i=0;i<choose;i++) x[i]=document.getelementbyid("cont"+i).style.visibility="visible"; function hola() { for(i=0;i<choose;i++) document.getelementbyid("cont"+i).style.visibility="visible"; } function verify(){ for(i=0;i<choose;i++){ xar[i]=document.getelementbyid("usernumber"+i).value; } for(i=0;i<choose;i++) if(xar[i]==i) alert("good"); else alert("wrong"); }
<input type="button" onclick="hola()" value="hidefield0" id="boton0"> <div id="cont0" style="visibility: hidden;"> <input type="text" id="usernumber" class="something"> </div> <input type="button" onclick="hola()" value="hidefield2" id="boton1"> <div id="cont1" style="visibility: hidden;" class="something"> <input type="text" id="usernumber2"> </div> <input type="button" onclick="hola()" value="hidefield1" id="boton2"> <div id="cont2" style="visibility: hidden;" class="something"> <input type="text" id="usernumber3"> </div> <input type="button" onclick="hola()" value="hidefield3" id="boton3"> <div id="cont3" style="visibility: hidden;" class="something"> <input type="text" id="usernumber4"> </div> <input type="button" onclick="hola()" value="hidefield4" id="boton4"> <div id="cont4" style="visibility: hidden;" class="something"> <input type="text" id="usernumber5"> </div> <input type="button" onclick="hola()" value="hidefield5" id="boton5"> <div id="cont5" style="visibility: hidden;" class="something"> <input type="text" id="usernumber6"> </div> <input type="button" id="verifybtn" value="verify" onclick="verify()">
Comments
Post a Comment