Changing Style of html element using js, | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Changing Style of html element using js,

I used, var x= document.getElementById("element name").style; x.cssproperty=new value; its saying Null , what's the possible cause

27th Aug 2017, 2:51 PM
ASHISH PANDEY
ASHISH PANDEY - avatar
5 Answers
+ 4
you should say var x = document.getElementById("elementName"); x.style.color = "red" this should work for texts ...and remember to wrap it up in window.onload = function(){ //your code goes here }
27th Aug 2017, 2:58 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 2
put it inside window.onload = function(){ } eg: https://code.sololearn.com/WDz18uXm8N3c/?ref=app ...if the problem persists, you can paste the code here.
27th Aug 2017, 3:10 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 1
Here is a simple and pretty cool example 😃 <!---✓ HTML (Hyper Text Markup Language)--> <div id ="demo">SOLOLEARN</div> <button onclick ="myFunction()">CHANGE</button> <script> // ✓ JS (JavaScript) function myFunction() { var x = document.getElementById("demo"); x.style.color = "red"; x.style.backgroundColor = "yellow"; x.style.fontSize = "30px"; } </script> 😃 KEEP CODING.
27th Aug 2017, 3:41 PM
Ankit
Ankit - avatar
0
its saying cannot read Style proper ty
27th Aug 2017, 3:04 PM
ASHISH PANDEY
ASHISH PANDEY - avatar
0
am using click event function
27th Aug 2017, 3:05 PM
ASHISH PANDEY
ASHISH PANDEY - avatar