469,323 Members | 1,516 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

DOM innerHTML "runtime error" in IE 6

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
1 2446
acoder
16,027 Expert Mod 8TB
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.

Similar topics

1 post views Thread by CARIGAR | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.