Using Form Information As a Variable using Javascript and Forms -
alright, people of stack overflow, have question you: in web design class @ high school , learning ahead of class since knew first half of class. asked teacher if teach have learned javascript , agreed. however, 1 of things wanted me teach not working me when try out on own. trying simple check variable when input name box, if name or teacher's name pulls popup says "welcome" or that, , if else says "go away" issue no matter try in code not working. test function have @ moment; intended print out
<!doctype html> <html> <head> <script type="text/javascript"> var name = document.kageform.user.value; function validator(){ alert(name); } </script> </head> <body> <form name="kageform"> username:<input type="text" name="user"> <br/> password: <input type="password" name="pass"> <br/> <input type="button"value="submit" onclick="validator()" /> </form> <script type="text/javascript"> </script> </body> </html>
here full version of code trying work:
<!doctype html> <html> <head> <script type="text/javascript"> var name = document.kageform.user.value; function validator(){ if(name=="kage kaldaka"){ alert("eeyup")}; else alert("nnope"); } </script> </head> <body> <form name="kageform"> username:<input type="text" name="user"> <br/> password: <input type="password" name="pass"> <br/> <input type="button"value="submit" onclick="validator()" /> </form> </body> </html>
you have several issues here:
- you have semicolon after
if
statement - you reading name value on page load, @ point when input field hasn't been added page yet , hasn't been filled out user yet. need read when user submits form, i.e. need move
name
assignment insidevalidator
method:
js:
function validator(){ var name = document.kageform.user.value; if(name=="kage kaldaka"){ alert("eeyup"); } else { alert("nnope"); } }
Comments
Post a Comment