-1

for some reason, I'm not able to set a variable of document.getElementById("id").style.color, as it's "null" apparently. Why do I have to call document.getElementById("id").style.color every time I need to set the color?

(I didn't have a picture of a light bulb, so I decided to improvise)

<html>
<head>
    <title>Hello</title>
</head>
<body>
    <pre id="light" onclick="toggleLight()">
      ____
    / __   \
   / //     \
  / ||       \
 |            |
 |     |O     |
  \    O|    /
   \   ||   /
    \      /
     |~~~~|
     |____|
       **
    </pre>
    <script>
        var on = false;
        var bulb = document.getElementById("light").style.color;
        function toggleLight(){
            if(on==true){
                bulb = "black";
                on=false
            } else if(on==false) {
                bulb = "yellow";
                on=true
            }
        }
    </script>
</body>

EDIT: It seems this wasn't clear. I edited the code, now you can try it. You'll know what I mean.

hexagonest
  • 612
  • 1
  • 10
  • 25
  • 1
    Dont know where you are facing an issue: THis seems to work: http://jsfiddle.net/FKtaD/ – karthikr Oct 28 '13 at 22:39
  • 1
    Seems to work for me here: http://jsfiddle.net/jfriend00/6Mcus/ – jfriend00 Oct 28 '13 at 22:40
  • 1
    I think they are saying they want to do: `bulb = document.getElementById("light").style.color` and then do `bulb = "black"` because it's effort to type it out each time. – Rhyono Oct 28 '13 at 22:41
  • You can't read property 'style' of null – bitoffdev Oct 28 '13 at 22:42
  • Yes, that is what I'm asking. Why does it set it to null if I do `bulb = document.getElementById("light").style.color`? – hexagonest Oct 28 '13 at 22:44
  • 1
    Basically, that is trying to fetch the value specified there. It's not grabbing that string and saving it for future calling. – Rhyono Oct 28 '13 at 22:45
  • So when you tell the bulb to equal black, it's not saying "ok, light, change your color" it's saying "my name is bulb and I am now containing the word 'black'." – Rhyono Oct 28 '13 at 22:47
  • GEEZ! You edited your code into an ENTIRELY different question with different code. Thanks for helping me to waste my time answering what you originally posted. – jfriend00 Oct 28 '13 at 22:55
  • @jfriend00 I edited it so it could be clearer. Calm down, it was your own decision to answer this question. – hexagonest Oct 28 '13 at 23:56
  • @DoorKnob - it's just that you completely changed the question so people that answered what you first had there wasted their time answering. That's just not a very efficient way to use this site (wasting people's time). Yes, it was my decision to answer your question, but I thought I was doing a service to you. Unfortunately, it wasn't possible. You didn't just clarify what was previously there - you completely changed the meaning of the code and the question. From all the working jsFiddle's it's obvious that I wasn't the only one that wasted my time. – jfriend00 Oct 29 '13 at 01:33
  • Also, next time you edit your question that changes it this much, please add a comment explaining what you did. We don't automatically notice when something has been edited unless there is a corresponding question. Thx. – jfriend00 Oct 29 '13 at 01:35
  • @jfriend00 I did add clarification. It's after my code. Also, I only changed two lines of it, because from what I saw it was just causing confusion to commenters. Sorry for the misunderstanding. – hexagonest Oct 29 '13 at 13:03

3 Answers3

3

The reason you can't save the color to a variable and then change it is because you will only change the variable.

You could however do this:

var elem=document. getElementById("light");

And then anywhere:

elem.style.color = "somecolor";

Loved the light bulb by the way.

amosmos
  • 1,039
  • 10
  • 20
2

You can get as far as setting var my_style = document.getElementById("light").style and then setting my_style.color = "black".

What you cannot do is set var color = document.getElementById("light").style.color and then set color = "black" because that will only change the value of the color variable and not the actual element.

This is because my_style stores a reference, while color stores a primitive. See Primitive value vs Reference value for further explanation.

Community
  • 1
  • 1
DJG
  • 6,413
  • 4
  • 30
  • 51
2

It now seems like the OP edited their question to change the code to something COMPLETELY DIFFERENT than they originally had. The answer below applies to what their code originally was, though my first code example also solves their issue.


Your script seems to work fine for a number of us (see referenced jsFiddle links in the comments to your question).

As to your other question: document.getElementById("light") fetches the DOM element that contains your light representation. You can either fetch that DOM element once and store it in a persistent variable such that you can just use that variable each time or you can simply fetch it each time you need it.

The general style guideline is that you don't want to persistently store DOM objects in long lasting variables unless you have a really good reason for doing so because in dynamic pages where objects are created and destroyed or in large complicated projects, storing a DOM reference in a persistent script variable can sometimes lead to either a memory leak or an inconsistent state. If you fetch the DOM object upon demand, you never get an inconsistent state or a leak.

But, there are more efficient ways to code what you've done that still request the DOM object upon demand such as this:

<script>
    var on = false;
    function toggleLight() {
        var bulb = document.getElementById("light");
        if (on) {
            bulb.style.color = "black";
        } else {
            bulb.style.color = "yellow";
        }
        on = !on;
    }
</script>

This style would fetch the DOM object only once ever, but it would then be storing that DOM object in a global variable which you generally want to avoid unless you have a very good reason for doing so:

<script>
    var on = false;
    var bulb = document.getElementById("light");

    function toggleLight() {
        if (on) {
            bulb.style.color = "black";
        } else {
            bulb.style.color = "yellow";
        }
        on = !on;
    }
</script>

The second script has a slight performance difference than the first (more startup execution time, less execution time at the time of the click), but since this is a user-triggered event, it would not be possible to notice the difference in execution time at the time of the click, therefore, the cleaner design of the first option is recommended (no storing of DOM objects in persistent variables).


FYI, notice that I've also shortened some of the rest of your code.

  1. if (on) in this case works just as well as if (on == true), but is more concise and is a more generally used style.
  2. There is no reason to use else if (on == false) when a simple else will work just as well for your specific case.
  3. I've replace the two separate assignments to on with a single assignment that simply reverses its state on = !on;.
  4. I've move the fetching of the DOM element outside the if/else statement since it is needed in both. Per the principles of DRY, you want to combine common code into a single place as often as practical.
jfriend00
  • 683,504
  • 96
  • 985
  • 979