By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,780 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

DOM innerHTML "runtime error" in IE 6

P: 4
Hi,

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
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.  
  5.         <script type="text/javascript">
  6.  
  7.             function validate_form()
  8.             {    
  9.  
  10.                 var x;
  11.                 for (i=0; i<document.registerForm.elements.length; i++)
  12.                 {
  13.                      x = document.registerForm.elements[i];
  14.  
  15.                     if (x.type != "button" && x.type != "submit" && x.type != "hidden" && x.value == "")
  16.                     {
  17.                         //var fieldName = x.name;
  18.                         var obj=document.getElementById('name');
  19.                         if(obj){
  20.                          alert("found");
  21.                         }
  22.  
  23.                         obj.innerHTML = "Visit W3Schools";
  24.  
  25.                         alert("false");        
  26.                     }
  27.  
  28.                 }
  29.  
  30.                 alert("true");    
  31.  
  32.             }
  33.         </script>
  34.     </head>
  35.     <body>
  36.  
  37.                 <form name="registerForm"  >
  38.  
  39.                     <table>
  40.  
  41.                     <tr><td>Name:</td><td>   <input name="name" type="text" size="35" />   <span id="name">www</span> </td></tr>
  42.  
  43.  
  44.                     <tr><td>Age:</td><td><input name="age" type="text" size="35" /><span id="age"></span></td></tr>
  45.  
  46.  
  47.                     <tr><td>Phone:</td><td><input name="phone" type="text" size="35" /><span id="phone"></span></td></tr>
  48.  
  49.  
  50.                     <tr><td>Email:</td><td><input name="email" type="text" size="35" /><span id="email"></span></td></tr>
  51.  
  52.  
  53.                     <tr><td colspan="2"><br/><br/><input type="button" onclick="validate_form()"  /></td></tr>
  54.  
  55.                     </table>
  56.                 </form>
  57.  
  58.     </body>
  59. </html>
Jan 24 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
If this is occurring in IE6 only, it will most likely be caused by the "name" id. Change it to something else, e.g. "nameValidation".
Jan 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.