Jump to content

Why me and not others? This line only works if it is below the HTML element....


bobleny

Recommended Posts

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?  ???

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.