+ 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

10th Apr 2020, 10:02 PM
David
6 Respostas
+ 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."
10th Apr 2020, 10:18 PM
Solo
Solo - avatar
+ 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.
11th Apr 2020, 1:01 AM
ionuț cosmin
ionuț cosmin - avatar
+ 1
10th Apr 2020, 10:20 PM
BroFar
BroFar - avatar
+ 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'; }
10th Apr 2020, 10:31 PM
Solo
Solo - avatar
+ 1
David I have always found putting it in a function is the easiest way rather onload or by button
11th Apr 2020, 12:59 AM
BroFar
BroFar - avatar
0
so i have to put it in a function?
11th Apr 2020, 12:06 AM
David