More Javascript Help

dwarfdude77

In Runtime
Messages
270
Location
USA
I keep getting this error:
Uncaught TypeError: Cannot set property 'innerHTML' of null

HTML:
Code:
<html>
<head>
	<title> Lines of Story </title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<script src="script.js"></script>
</head>

<body>
	<p id="demo"> Hi</p>



</body>
</html>

Javascript:
Code:
var p= document.getElementById("demo");

p.innerHTML="hello";
 
You need to be calling your JS in some sort of event that's ran after the HTML is rendered/ready.

Most likely what's happening is that your JS is firing first and failing because there is no HTML element with the ID "demo" being rendered on the screen yet.

Edit: Just tried it on JSFiddle... putting it in the head doesn't work. however, if you call that part of the script in the onLoad event...it works fine. For the reason that I stated above.
 
You need to be calling your JS in some sort of event that's ran after the HTML is rendered/ready.

Most likely what's happening is that your JS is firing first and failing because there is no HTML element with the ID "demo" being rendered on the screen yet.

Edit: Just tried it on JSFiddle... putting it in the head doesn't work. however, if you call that part of the script in the onLoad event...it works fine. For the reason that I stated above.

Ahh, okay, yes i called a function with the onload event in the body. Thanks.
 
Okay, need more help please, this time with if-else statements.

Code:
function change() {var p= document.getElementById("main");

if(p.innerHTML="This is a text based game."){

	p.innerHTML="It will be extremely boring.";

}else{ 
	p.innerHTML="This is a text based game."
}

}

The text inside the paragraph with the id "main" is completely different than "It will be extremely boring" or "This is a text based game". Now, I want it to go from the original text to "This is a text based game" , and then, when the button is clicked again, to check to see if the text is already "This is a text based game" and say, yes this is true, so it would change the text to "it will be extremely boring",

But when I click the button for the first time, it just goes right to "It will be extremely boring", and skips over "This is a text based game"

I do not get any errors in chrome

Please help!
 
You have a logical error.

JavaScript Comparison and Logical Operators

You're using the wrong operator in your if statement - you need to use a double equals ( == ), instead of a single equals ( = ).

Single equal sign means assignment - so you're assigning p.innerHTML to that value, and since that simply returns true, it goes into "It will be extremely boring"

Double equals means comparison, so it will compare the values and then return true/false.
 
You have a logical error.

JavaScript Comparison and Logical Operators

You're using the wrong operator in your if statement - you need to use a double equals ( == ), instead of a single equals ( = ).

Single equal sign means assignment - so you're assigning p.innerHTML to that value, and since that simply returns true, it goes into "It will be extremely boring"

Double equals means comparison, so it will compare the values and then return true/false.

Ahhh okay then I remember now thanks.
 
Ahhh okay then I remember now thanks.

I hate that error probably the most lol. Tends to be hard to find - would run into it occassionally when I was in school and doing C++. Now that I'm using C#, I don't run into that error anymore because it's a syntax/compiler error instead of a logical error that doesn't get caught.
 
Back
Top Bottom