Changing Style of html element using js, | Sololearn: Learn to code for FREE!


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

8/27/2017 2:51:51 PM


5 Answers

New Answer


you should say var x = document.getElementById("elementName"); = "red" this should work for texts ...and remember to wrap it up in window.onload = function(){ //your code goes here }


put it inside window.onload = function(){ } eg: ...if the problem persists, you can paste the code here.


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"); = "red"; = "yellow"; = "30px"; } </script> 😃 KEEP CODING.


its saying cannot read Style proper ty


am using click event function