0

I'm working on adding javascript functionality to a basic HTML page with multiple buttons. The code works when I include one function in my external JS file, but stops working when I try to add another piece of button functionality.

How do I fix this issue?

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Jiggle Into JavaScript</title>
    <!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.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>

    <script type="text/javascript" src="javascript.js"></script>

</body>
</html>

Snippet:

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

document.getElementById("button2").addEventListener("click", function(){
  document.getElementById("box").style.background-color = "blue";
});
<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>

1
  • Did you bother opening the Javascript console? It would have told you where the error was. Commented Aug 9, 2017 at 12:46

4 Answers 4

6

You have incorrect syntax here:

document.getElementById("box").style.background-color = "blue";

- is the subtraction operator, it can't be used as part of a property literal. Style properties are created by converting the CSS name to camelCase, so it should be:

document.getElementById("box").style.backgroundColor = "blue";
Sign up to request clarification or add additional context in comments.

Comments

0

You can also used simple background

document.getElementById("box").style.background = "blue";

Comments

0

Since you have tagged JQuery too, here is the JQuery version. In JQuery, you can use css() where you can add/change multiple CSS properties:

$("#button1").on('click', function() {
  $("#box").css({
    'height': '500px'
  });
})

$("#button2").on('click', function() {
  $("#box").css({
    'background-color': 'blue'
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <title>Jiggle Into JavaScript</title>
  <!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.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>

  <script type="text/javascript" src="javascript.js"></script>

</body>

</html>

Comments

0

In case you wanted a JQuery answer here is one too.

$('button').click(function() {
    var $box = $('#box');
    if($(this).attr('id') == 'button1')
    {
        $box.css('height','500px');
    }
    else if ($(this).attr('id') == 'button2')
    {
        $box.css('background-color', 'blue');
    }
});

Comments

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.