Here is the code:

function fastmenu() { let menu = document.getElementById('actions'); if (menu.style.opacity != 1) { return menu.style.opacity = 1; } menu.style.opacity = 0; } 

tried to do so

 let menu = document.getElementById('actions').style.opacity; 

so:

 let menu = document.getElementById('actions'); let opacity = menu.style.opacity; 

Does not work. Your view on why this is not working and how can this be corrected?

Closed due to the fact that the essence of the issue is incomprehensible to the participants Grundy , Yaroslav Molchan , zb ' , user207618, aleksandr barakin 3 Jun '17 at 22:44 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Why are there so many cons and not a single comment? Why is the question bad? - Dmitry
  • @Dmitry there were 20 of them, they were erased - Alex78191
  • one
    @Dmitry cons - participants' reaction to the behavior of the author of the question in the comments - PashaPash

1 answer 1

Objects are copied by reference, and primitives by value. In your first case, you copy the link to the object representing the DOM element to the menu variable; by changing the properties of the menu variable, you change the properties of this element. In the second and third cases, I will assume that you are most likely copying the value of the opacity property, which is not an object, into a variable, so by changing the value of this variable, you have no influence on anything.

In short, you can write through the ternary operator, but essentially it does not change.

 function fastmenu() { let menu = document.getElementById('actions'); menu.style.opacity = menu.style.opacity != 1 ? 1 : 0; } 
  • Bartender! Pour a glass of some coffee at my expense `/ - Name
  • @Name, yes that there are two grants to this gentleman! - user207618