Change color second button with java script

change button color onclick javascript
change background color using javascript function
change background color on button click in javascript
how to change button color in javascript
change button color onclick css
change background color on button click in html
javascript change background color based on value
change button color onclick jquery

I have two Red buttons. If you click a button the color should change to Green. If you click again it should return to Red. Now I succeed to change the color of the first button, but not of the seccond button. Has someone an idea?

I have already a java script that change the color of one button

var button = document.querySelector("button");

button.addEventListener("click", function() {
  const curColour = button.style.backgroundColor;

  if (curColour === 'red') {
    button.style.backgroundColor = "green";
  } else {
    button.style.backgroundColor = "red";
  }
});
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background: red
}

.button1:hover {
  background-color: green;
}
<button id="bGeneral" class="button1" name="bGeneral"><b>General</b></button>
<!-- Create extra space -->
<p><br></p>
<!-- The Next Button Plates -->
<button id="buttonP" class="button1" name="buttonP"><b>Plates</b></button>

As Andreas already said in the comments. You need querySelectorAll instead of querySelector. That will give you a collection which you must loop through

var buttons = document.querySelectorAll("button");
for (const button of buttons) {
    // ...
}

var buttons = document.querySelectorAll("button");

for (const button of buttons) {
  button.addEventListener("click", function() {
    const curColour = button.style.backgroundColor;

    if (curColour === 'red') {
      button.style.backgroundColor = "green";
    } else {
      button.style.backgroundColor = "red";
    }
  });
}
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background: red
}

.button1:hover {
  background-color: green;
}
<button id="bGeneral" class="button1" name="bGeneral"><b>General</b></button>
<!-- Create extra space -->
<p><br></p>
<!-- The Next Button Plates -->
<button id="buttonP" class="button1" name="buttonP"><b>Plates</b></button>

How to change the background color after clicking the button in , Approach 1: This approach uses JavaScript to change the background color after clicking the button. Use HTML DOM Style backgroundColor Property to change the background color after clicking the button. This property is used to set the background-color of an element. Given an HTML document and the task is to change the background color of the document using JavaScript and jQuery. Approach 1: This approach uses JavaScript to change the background color after clicking the button. Use HTML DOM Style backgroundColor Property to change the background color after clicking the button. This property is used to set the background-color of an element.

You can use toggleClass. Also use querySelectorAll this will give all the buttons. Then iterate this collection and add event listener to it. .Inside callback function use classList.toggle to add or remove the class

var button = [...document.querySelectorAll("button")].forEach((item) => {
  item.addEventListener("click", function() {
    this.classList.toggle('toggleButtonColor')
  });

})
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background: red
}

.button1:hover {
  background-color: green;
}

.toggleButtonColor {
  background: green;
}
<body style="background-color:#E3CEF6;">

  <button id="bGeneral" class="button1" name="bGeneral"><b>General</b></button>
  <!-- Create extra space -->
  <p><br></p>
  <!-- The Next Button Plates -->
  <button id="buttonP" class="button1" name="buttonP"><b>Plates</b></button>
</body>

Changing HTML button text color multiple times with JS function , I am trying to write a function that will change a button's text color to red on the first click, green on the second click and back to black on the third� Change background color every seconds in JavaScript with a random hex color. setInterval ( function () { var randomColor = Math.floor (Math.random ()*16777215).toString (16); document.body.style.backgroundColor = "#"+randomColor; },1000); Here I have used 1000 milliseconds because I want to change the background color in every 1 second.

You are using two different way to deal with the click. Use either onclick or addEventListener. Here is an example.

I haven't improved the inside of your function tho. Look at @brk answer for opti

function showOrHide(id, name) {
  const button = document.getElementById(id);

  const curColour = button.style.backgroundColor;
  
  if (curColour === 'red' || !curColour) {
    button.style.backgroundColor = 'green';
  } else {
    button.style.backgroundColor = 'red';
  }
}
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background-color: red;
}

.button1:hover {
  background-color: green;
}

body {
  background-color: #E3CEF6
}
<button id="bGeneral" onclick="showOrHide(this.id, 'General')" class="button1" name="bGeneral">
<b>General</b></button>
<!-- Create extra space -->
<p><br></p>

<!-- The Next Button Plates -->
<button id="buttonP" onclick="showOrHide(this.id, 'Plates')" class="button1" name="buttonP"><b>Plates</b></button>

How to Change Button Color OnClick in Javascript, KodeBase. 2.66K subscribers. Subscribe. In this video tutorial, you will learn how to change Duration: 4:21 Posted: Nov 11, 2019 At first I thought you were trying to change the color of all the buttons because you were using getElementsByTagName but it looks like you just want to change the color of the button that was pressed. In that case you don't need to use an array. Just pass the element that was clicked to the function and then change that specific button's color.

you can use id :

function showOrHide(id) {
  var element = document.getElementById(id);
  const curColour = element.style.backgroundColor;
  if (curColour === 'red') {

        element.style.backgroundColor = "green";
    }
    else {
        element.style.backgroundColor = "red";
    }
}

and in HTML:

<button  id="bGeneral" onclick="showOrHide(this.id)" class="button1"  name= "bGeneral" ><b>General</b></button> 
  <!-- Create extra space -->
<p><br></p> 
<!-- The Next Button Plates -->
<button id = "buttonP" onclick="showOrHide(this.id)" class="button1" name= "buttonP" ><b>Plates</b></button> 

How to change the background color of a web page using JavaScript, You can easily change the background color of a webpage i.e. the <body> element or any other element dynamically by using its style property in JavaScript . <button type="button" onclick="changeBodyBg('yellow');">Yellow</button> Change Background Color On Button Click in JavaScript. Similarly, we will also use the Javascript event on the button main event learn JQuery Radio Button Checked Event. we can use this event anywhere. We show you how to use a button event on page with the function and change background color change. This is button code for onclick event.

This is the proper way with your thoughts

<!DOCTYPE html>
    <html>
    <head>
     <style>
    button {
      height:40px;
      width:160px;
      border: 4px;
      border-radius: 20px 20px 20px 20px;
      border-color:red;
      color: yellow;
      padding: 12px 15px;
      text-align: center;
      font-size: 16px;
      cursor: pointer;
    }
    .button1 { background: red }

    .button1:hover {
      background-color: green;
    }

    </style>
    </head>
     <body onload="beginfase()" style="background-color:#E3CEF6;" >

    <button  id="bGeneral" onclick="" class="button1"  name= "bGeneral" ><b>General</b></button> 
      <!-- Create extra space -->
    <p><br></p> 
    <!-- The Next Button Plates -->
    <button id = "buttonP" onclick="" class="button1" name= "buttonP" ><b>Plates</b></button> 

    <script type="text/javascript">
    //we are creating an array
    var button = [];
    button = document.querySelectorAll("button");
    //we are binding the function to all the elements of the array
    for(i=0;i<button.length;i++){
        button[i].onclick = function(){
    // this represent the elemement which is being clicked
          if(this.style.backgroundColor === "red"){
            this.style.backgroundColor = "green"
          }
          else{
          this.style.backgroundColor = "red"
          }

        }
    }

    </script>
      </body>

JavaScript HTML DOM - Changing CSS, The HTML DOM allows JavaScript to change the style of HTML elements. of this tutorial. This example changes the style of the HTML element with id="id1" , when the user clicks a button: Change the text color of the <p> element to "red". I want to change the color of a button when it is clicked and have added this getElementById line in the function called by onclick but when I add the line the function won't run.

JavaScript String fontcolor() Method, The fontcolor() method is used to display a string in a specified color. This method returns the string embedded in the <font> tag, like this: <font color=" colorvalue">� // Get the button, and when the user clicks on it, execute myFunction document.getElementById("myBtn").onclick = function() {myFunction()}; /* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */

HTML DOM Style borderColor Property, Change the color of the four borders of a <div> element to red: document. getElementById("myDiv").style.borderColor = "red";. Try it Yourself �. More "Try it � If you mean changing the color by calling javascript code then you can do this: <button id = "myButton" onClick = "changeButtonColor ()">click to change color</button>. <script>. function changeButtonColor () {. document.getElementById ("myButton").style.backgroundColor = "green";

jQuery toggle() Method, Toggle between colors when clicking on a <p> element: an element, the first specified function fires, when clicking again, the second function fires, and so on. In this example, we will discuss that how we change the Background Color of a Button with JavaScript. Tune in FREE to the React Virtual Conference Sep. 11 at 10am ET x LEARN: React Virtual Conference

Comments
  • .querySelector() always returns the first matched element. Use .querySelectorAll() -> documentation
  • Good answer :) It's little odd tho use spread operator along with var. Either it's ES6, either it's not.
  • In every modern browser (all but IE...) a NodeList has a .forEach() method so the conversion into an actual array is not necessary.