Jordan Rabideau
Jordan Rabideau

Reputation: 11

Change the color of a div

I'm new to coding so I am doing some practice exercises in which I need to use javascript to alter the image of a box in an html file. Everything seems to be working except for the "blue" button, even though I have used the same syntax as in all the other scripts. Can anyone tell me what I'm missing?

document.getElementById("button1").addEventListener("click", function() {

    document.getElementById("box").style.height = "250px"

});

document.getElementById("button2").addEventListener("click", function() {

    document.getElementById("box").style.color = "blue"

});

document.getElementById("button3").addEventListener("click", function() {

    document.getElementById("box").style.opacity = 0.5

});

document.getElementById("button4").addEventListener("click", function() {

    document.getElementById("box").style.height = "150px"

    document.getElementById("box").style.opacity = 1

    document.getElementById("box").style.color = "orange"

});
<!DOCTYPE html>
<html>
<head>
    <title>Javascript Intro</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>

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

</body>
</html>

Upvotes: 1

Views: 1409

Answers (3)

ali ghasemzad
ali ghasemzad

Reputation: 66

You should change .color to .backgroundColor as others explained.

Also, it's good to mention that you could use jQuery like this: (I think jQuery's method is easier to read and work, especially if you want to change more than one attr at the time)

<script>
$(document).ready(function() {
    $("#button1").click(function(){
        $("#box").css('height', '250px');
    });

    $("#button2").click(function(){
        $("#box").css('background-color', 'blue'); 
    });

    $("#button3").click(function(){
        $("#box").css('opacity', '0.5');
    });

    $("#button4").click(function(){
        $("#box").css('opacity', '1')
                 .css('background-color', 'orange')
                 .css('height', '150px');
    });

});
</script>

Upvotes: 0

Narendra
Narendra

Reputation: 4574

document.getElementById("button2").addEventListener("click", function() {
    console.log(document.getElementById("box"));
    document.getElementById("box").style.backgroundColor = "blue";
    document.getElementById("box").style.color = "red";

});

and

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

will show you text in red.

Upvotes: 0

Darren
Darren

Reputation: 718

Change this:

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

To this:

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

And it should be fine. Make sure you do the same for your reset button.

The reason this works is because color and backgroundColor change 2 different things. color changes text color, and backgroundColor changes the element's background color.

Hope that helps.

Upvotes: 4

Related Questions