2

I am quite new to HTML/CSS/JavaScript and I was wondering how I could change the background colour, with a combination of JavaScript? I have 3 buttons (red, green and blue), and whenever the user presses on one of the three buttons, the background should change accordingly.

Currently, only the 'red' button works. The background doesn't change for the blue and green buttons.

Thanks in advance!

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <button id = "red" value = "red">R</button>
        <button id = "green" value = "green">G</button>
        <button id = "blue" value = "blue">B</button>
        <script>
            document.querySelector('button').onclick = function() {
                document.querySelector('body').style.backgroundColor = this.value;
            }
        </script>
    </body>
</html>
distante
  • 6,438
  • 6
  • 48
  • 90

3 Answers3

1

Use: document.querySelectorAll, then loop through selected elements with document.queryselectorall

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <button id = "red" value = "red">R</button>
        <button id = "green" value = "green">G</button>
        <button id = "blue" value = "blue">B</button>
        <script>
            document.querySelectorAll('button').forEach(function(element) {
                element.onclick = function() {
                    document.body.style.backgroundColor = this.value;
                }
            })
        </script>
    </body>
</html>

Small note: Use document.body (docs) instead of document.querySelector('body') to speed things up ;)

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    `document.querySelector('body')` should be stored outside of the listener for better performance. – marzelin Aug 21 '20 at 11:28
  • 1
    btw, you don't have query for `body`, use `document.body`. – marzelin Aug 21 '20 at 11:38
  • I've used `document.querySelctor` because OP was using it, shameful copy paste ;) – 0stone0 Aug 21 '20 at 11:49
  • Could the silent down-voter please clarify how I should improve my answer? – 0stone0 Aug 21 '20 at 13:16
  • Thanks for you answer. What exactly does 'function(element) do? Does that refer to one line, e.g. ? – user13717018 Aug 23 '20 at 08:15
  • Your welcome! Please [accept/upvote](https://stackoverflow.com/help/someone-answers#:~:text=To%20accept%20an%20answer%3A,the%20answer%2C%20at%20any%20time.) the answer if it helped you. The `function(element)` is described here: [`forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach), it's each element thats in the array. – 0stone0 Aug 23 '20 at 15:09
0

That's because

document.querySelector('button').onclick

Only adds the event to the first button, in this case red

The are a few ways to do it for all of them, one way is

Array.from(document.querySelectorAll('button')).forEach(x=> x .onclick = function() {
                document.querySelector('body').style.backgroundColor = this.value;
            })
0

Your code not working because querySelector() method returns only the first element that matches. For further details, check here: w3schools.com

Check out this snippet:

<!DOCTYPE html>

<html lang="en">
<head>
    <title>My Webpage</title>
    <script type="text/javascript">
        function changeBackground(button){
            document.body.style.backgroundColor = button.value;
        }
    </script>
</head>
<body>
    <button id = "red" value = "red" onclick="changeBackground(this);">R</button>
    <button id = "green" value = "green"onclick="changeBackground(this);">G</button>
    <button id = "blue" value = "blue"onclick="changeBackground(this);">B</button>

</body>
</html>

Here we are calling changeBackground() method with current button as parameter on button click. In the method, value of clicked button is used to set style of document's body.

gpl
  • 1,380
  • 2
  • 8
  • 24