Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I haven't coded in Javascript directly in almost 3 years and I'm just recently jumping back into it. That being said, I can not for the life of me figure out why this isn't working.

HTML
<!DOCTYPE html>
<html>
<head><title>Hello</title><script>
function changeH1Text()
{
document.getElementById("H1").innerHTML = "I've been changed";
}
</script></head>

<body>

<h1 id="H1"  önload="changeH1Text()">Welcome to Javascript</h1>
<button type="button" onclick="document.getElementID("body").Text = 'The button changed me';">Click Me!</button>
</body>
</html>
Posted

C#
<html>
<head>
	<title>Hello</title>
	<script>
	function changeH1Text()
	{
		document.getElementById("H1").innerHTML = "I've been changed";
	}
	</script>
</head>
 
<body id="body"  önload="changeH1Text();">
	<h1 id="H1">Welcome to Javascript</h1>
	<button type="button"  önclick="document.getElementById('body').innerHTML = 'The button changed me';">Click Me!</button>
</body>
</html>


I am recommending you to use FireBug. Its really handy plugin ;)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Aug-13 10:19am    
5ed.
—SA
Hi Sergey,

This will still not work, as body is not a Id of any control in the page, rather it is a tag.
I mentioned this issue in my solution as well.

Thanks,
Tadit
patrik polakovic 2-Aug-13 10:39am    
"body" is ID of tag <body> ...
Yeah, sorry.... My Apologies. I missed that...

+5ed... Sorry again. My mistake. :)

Also check my answer, where I have explained OP about the issues.
Sorry Sergey, my mistake... he has given Id to body tag as body. So, it i working... :)
Problem
If you check the console tab of FireBug in FireFox, you can see the following issue there.
Quote:

SyntaxError: syntax error

document.getElementID(

ThirdPage.aspx (line 1, col 22)


It clearly mentions that error is at document.getElementID(.
And here are two problems.

  • That is document.getElementByID not document.getElementID.
  • And there is no element in your code, which has a Id as body, it is rather a tag.


Solution
So, get correct Id of the control you want to use in the code with document.getElementByID.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-13 10:19am    
5ed. Not only the answer but also the advice how to deal with problems...
—SA
Thanks a lot Sergey Alexandrovich Kryukov... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900