focus on input field |
focus on input field |
John Kale |
Feb 19 2009, 11:17 PM
Post
#1
|
Group: Members Posts: 1 Joined: 19-February 09 Member No.: 7,850 |
Can anybody tell me what is i am doing wrong with the following code. all i want is if the name field is bland then it should give an alert and then focus on the input field, but what its doing is it gives the alert and then focus and then refresh the page.
CODE <html> <head> <script> function checkform(){ if (document.form1.name1.value.length<1) { window.alert("enter your name") document.form1.name1.focus()} else{ window.open("http://www.msn.com")} } </script> </head> <body> <form name="form1"> <input type="text" name="name1" size="35"> <input type="submit" name="submit" value="Submit" OnClick="checkform()"> </form> </body> </html> |
Brian Chandler |
Feb 20 2009, 01:40 AM
Post
#2
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
You need to set the result of your checkform() function to TRUE or FALSE (sorry can't remember which) to prevent the submit from taking effect anyway.
|
geoffmerritt |
Mar 7 2009, 03:50 AM
Post
#3
|
Member Group: Members Posts: 66 Joined: 23-December 08 From: Adelaide Member No.: 7,394 |
Coding..... don't you hate it when you forget some of these ;
CODE if (document.form1.name1.value.length<1) { window.alert("enter your name") document.form1.name1.focus() } else { window.open("http://www.msn.com")} } Try this code CODE if (document.form1.name1.value.length<1) { window.alert("enter your name"); document.form1.name1.focus(); return false; } else { window.open("http://www.msn.com"); } |
Lo-Fi Version | Time is now: 18th April 2024 - 07:27 PM |