1

My assignment is to modify the box provided by the instructor using Javascript. However, none of the changes I make on my Javascript file seem to work. I have named my HTML file index.html and Javascript file javascript.js. Here is what I have:

document.getElementById("button1").addEventListener("click", function(){
    document.getElementById("box").style.height = "250px";

        });

document.getElementById("button2").addEventListener("click", function(){
    document.getElementById("box").style.color:orange

        });

 document.getElementById("button3").addEventListener("click", function(){
    document.getElementById("box").style.opacity = 0;

        });
document.getElementById("button4").addEventListener("click", function(){
    document.getElementById("box").style.height = "150px"
<!DOCTYPE html>
<html>

<head>
  <title>Jiggle Into JavaScript</title>
  <script type="text/javascript" src="javascript.js">
  </script>
</head>

<body>

  <p>Press the buttons to change the box!</p>

  <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>

  <button id="button1">Grow</button>
  <button id="button2">Blue</button>
  <button id="button3">Fade</button>
  <button id="button4">Reset</button>


</body>

</html>

2
  • How do you ever get back the normal opacity? Commented Aug 11, 2020 at 1:14
  • The reset button should do that. But I still have to work on that. I suspect that all I have to do is add .addEventlistener and include all of the original styles, including opacity. Commented Aug 12, 2020 at 3:13

3 Answers 3

3

The problem is here: document.getElementById("box").style.color:orange . It should be document.getElementById("box").style.color = "orange";.

document.getElementById("button1").addEventListener("click", function(){
    document.getElementById("box").style.height = "250px";

});

document.getElementById("button2").addEventListener("click", function(){
    document.getElementById("box").style.color = "orange";

});

 document.getElementById("button3").addEventListener("click", function(){
    document.getElementById("box").style.opacity = 0;

});

document.getElementById("button4").addEventListener("click", function(){
    document.getElementById("box").style.height = "150px"
});
<!DOCTYPE html>
<html>

<head>
  <title>Jiggle Into JavaScript</title>
  <script type="text/javascript" src="javascript.js">
  </script>
</head>

<body>

  <p>Press the buttons to change the box!</p>

  <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>

  <button id="button1">Grow</button>
  <button id="button2">Blue</button>
  <button id="button3">Fade</button>
  <button id="button4">Reset</button>


</body>

</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Why is the button called BLUE making it orange? I feel like the instructor provided the JS and is testing the students.
0

Well, when I run the code snippet, stacks "mini google inspect" shows that the ":" in your javascript should not be there!

So your code should be

document.getElementById("button2").addEventListener("click", function(){
    document.getElementById("box").style.color = "orange";

        });

instead of what you currently have.

Comments

0

document.getElementById("button1").addEventListener("click", function(){
    document.getElementById("box").style.height = "250px";

        });

document.getElementById("button2").addEventListener("click", function(){
    document.getElementById("box").style.background = "blue";

        });

 document.getElementById("button3").addEventListener("click", function(){
    document.getElementById("box").style.opacity = 0;

        });
document.getElementById("button4").addEventListener("click", function(){
    document.getElementById("box").style.height = "150px";
  });
<body>

  <p>Press the buttons to change the box!</p>

  <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px;opacity: 1;"></div>

  <button id="button1">Grow</button>
  <button id="button2">Blue</button>
  <button id="button3">Fade</button>
  <button id="button4">Reset</button>


</body>

Check your syntax :)

2 Comments

I see that running the code snippet on here works. But when I try to run on my end by using the default browser, it doesnt work. Any suggestions?
@Moises, you should check your syntax of document.getElementById("box").style.color:orange to document.getElementById("box").style.color = "orange"; and always check your close parenthesis or close brackets on your last line of your provided code });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.