bobleny Posted June 10, 2007 Share Posted June 10, 2007 What is the difference between this: <html> <head> <script type="text/javascript"> document.getElementById('login_button').innerHTML = "George"; </script> </head> <body> <div class='right_panel_box'> <div class='right_panel_text_lable'>Login</div> <form action=''> <div class='login_input'><input type='text' name='username' size='15' value='Username' onkeyup='CheckUsername(This.Value) /></div> <div id='check_username'></div> <div class='login_input'><input type='password' name='password' size='15' value='Password' /></div> <div id='check_password'></div> <div id='login_button'></div> </form> </div> </body> </html> and this: <html> <head> </head> <body> <div class='right_panel_box'> <div class='right_panel_text_lable'>Login</div> <form action=''> <div class='login_input'><input type='text' name='username' size='15' value='Username' onkeyup='CheckUsername(This.Value) /></div> <div id='check_username'></div> <div class='login_input'><input type='password' name='password' size='15' value='Password' /></div> <div id='check_password'></div> <div id='login_button'></div> </form> </div> <script type="text/javascript"> document.getElementById('login_button').innerHTML = "George"; </script> </body> </html> Hmm? Let me give you a clue, one works, and the other doesn't. I don't get it, it should. It's supposed to write "George", in the login_button <div> tag. I have another script that works, I just don't get it. Why do I have to have the line below the element, and other people don't? I'm confused... Any ideas? ??? Quote Link to comment Share on other sites More sharing options...
nicephotog Posted June 11, 2007 Share Posted June 11, 2007 The code ......onkeyup='CheckUsername(This.Value)...... should have another single quote inside the html element to close the listener, It should not operate in either so you are doing well to say the least since it is an error. As for the code not operating, Do not use document.getElementById from the head of the HTML document because it is out of scope unless it is returned as part of a function or used in an object. One more detail, If you did not know it , a DIV is almost another document body in the document hierarchy layed out by the script engines and for effect of scope it is. Two things always need to occur, Always put a script after the HTML in the body and the old method of accessing a DIV is document.getElementById('MYDIV').document.getElementById('insidediv2).style.visibility='hidden'; Quote Link to comment Share on other sites More sharing options...
nogray Posted June 11, 2007 Share Posted June 11, 2007 The first script will run the Javascript before the browser writes the div tag, so it will look for the div tag but can't find (error, the object haven't loaded) The second one will run the script after. To be safe, always make sure your page loads before trying to access element. You can add an onload even in the body tag and run a function from there <head> <script type="text/javascript"> function write_UN(){ document.getElementById('login_button').innerHTML = "George"; } </script> </head> <body onload="write_UN();"> Also, This.value should be this.value (lower case letters) Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 11, 2007 Share Posted June 11, 2007 nogray is right, but i would take away that body onload element and do this in the header: onload = function(){ //put your function calls here } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.