Moises
Moises

Reputation: 11

Editing HTML DOM using Javascript

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>

Upvotes: 1

Views: 120

Answers (3)

MikeE
MikeE

Reputation: 1

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 :)

Upvotes: 0

coding_makes_no_sense
coding_makes_no_sense

Reputation: 35

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.

Upvotes: 0

shreyasm-dev
shreyasm-dev

Reputation: 2834

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>

Upvotes: 3

Related Questions