+ 1
Changing text color with JavaScript
What is wrong with my JS that it won’t change the color to blue? https://code.sololearn.com/WLbw0Jc4cAUf/?ref=app
6 odpowiedzi
+ 2
1. Remove the double quotation marks (" ").
2. Move the "script" from the "head" to the very bottom of the "body".
P. S: "Your script loads earlier than the page, so it cannot determine your paragraph."
+ 2
<body>
<p id="change_color">
text was red now blue.
</p>
<script>
var els = document.getElementById('change_color');
els.style.color = 'blue';
</script>
</body>
There was get eleme t by id inside "quotes" and scrip tag was before the paragraf. Now is fixed.
+ 1
Watch the text change onclick https://code.sololearn.com/Wfl7z7583TFW/?ref=app
+ 1
Actually, it’s better to write scripts in a separate js file, your code there will look like this:
onload=()=>{
var els = document.getElementById ('change_color');
els.style.color = 'blue';
}
+ 1
David I have always found putting it in a function is the easiest way rather onload or by button
0
so i have to put it in a function?