Please check the error in my javascript based html code

HTML

<!DOCTYPE html>

<html> 

<head>

	<link href = "calculator.css" rel = "stylesheet" type = "text/css"> 
	
<title>

	Calculator
	
</title>

</head>



<body id = "body">
	

	<p id = "p1"> hi there this is some sort of paragraph</p>


<script src = "calculator.js" type = "text/javascript"> 

    </script>


</body>



</html>

JAVASCRIPT CODE

var p1 = document.getElementById("p1");

p1.style.background = "green";

here, I tried giving the p1 paragraph to just check out whether my file was linked to the HTML file here i set the style property of the p1 paragraph to green but instead it did not change. please tell me why.

1 Like

Hi @amazingmathsworld864
sorry for the stupid question: Are you sure your js file is in the same folder as your index.html and that there is no typo in the file name?
Then try to add an event listener and add a console like this:

document.addEventListener('DOMContentLoaded', function() {
    console.log('document is ready.');
    var p1 = document.getElementById("p1");

    p1.style.background = "green";
});

But since you load your js in the body it’s unlikely that that’s the cause of the problem.
Also remove the spaces around the ‘=’.

<script src="calculator.js" type="text/javascript">

yes it is in the same file and that didn’t work

reckon there’s something wrong with the code editor?

Where did you check your file? Didn’t you check it in a browser? I’m sure there’s nothing wrong with the editor. Did you also remove all the spaces?

Try backgroundColor instead of background.

Also check if there is any error (actual error, not simply “not working”)

1 Like

thank you i was able to figure out the problem.

Would you mind telling us what the problem was?

2 Likes

it was a spelling mistake in one letter I use Grammarly so I think that Grammarly may have corrected it when I posted it here

2 Likes