Home > Back-end >  JS variables and css properties
JS variables and css properties

Time:10-09

If I put a css property in JS variable, and change the variable value, does the property change?

And a little subquestion: what's difference between let and var, and what should I use?

For example:

    let property = document.getElementById("el").style.display;
//  variable's content changed
    property = "block";
//  will the #el's display property change?
    

CodePudding user response:

    let property = document.getElementById("el").style.display;
//  variable's content changed
    property = "block";
//  will the #el's display property change?
    

No . What is changing is the Javascript variable called property. First of all it takes on the value of the style.display of the element #el and then it is given the value of another string which is 'block'

It is possible to set a property of el but on the 'left hand side' you need to tell JS that is what you want it to do:

document.getElementById("el").style.display = "block";
  • Related