The error I am describing occurs with the script I've pasted below.
It's a validator for form fields and prints a message into a span tag located beside the empty input tag.
I have hard coded the error message to be printed in a specific span tag for testing. --> "<span id="name">www</span>"
When this span tag is positioned BEFORE the input tag it works fine but when I place it after (which is the layout I want) it stops working and gives a "runtime error" message.
Any ideas what is causing this?
Any help would be greatly appreciated.
Thanks in advance, Will
Expand|Select|Wrap|Line Numbers
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script type="text/javascript">
- function validate_form()
- {
- var x;
- for (i=0; i<document.registerForm.elements.length; i++)
- {
- x = document.registerForm.elements[i];
- if (x.type != "button" && x.type != "submit" && x.type != "hidden" && x.value == "")
- {
- //var fieldName = x.name;
- var obj=document.getElementById('name');
- if(obj){
- alert("found");
- }
- obj.innerHTML = "Visit W3Schools";
- alert("false");
- }
- }
- alert("true");
- }
- </script>
- </head>
- <body>
- <form name="registerForm" >
- <table>
- <tr><td>Name:</td><td> <input name="name" type="text" size="35" /> <span id="name">www</span> </td></tr>
- <tr><td>Age:</td><td><input name="age" type="text" size="35" /><span id="age"></span></td></tr>
- <tr><td>Phone:</td><td><input name="phone" type="text" size="35" /><span id="phone"></span></td></tr>
- <tr><td>Email:</td><td><input name="email" type="text" size="35" /><span id="email"></span></td></tr>
- <tr><td colspan="2"><br/><br/><input type="button" onclick="validate_form()" /></td></tr>
- </table>
- </form>
- </body>
- </html>