How to change button color in javascript?

Maduro

I have 5 buttons and they are all blue. When you click on one of those button, the button that is clicked should change to red. But the other four buttons should remain in blue. In addition, if you click the button two or more times, it should not change the color back to blue. For example, If I click on button one it will change to red, if i click button one again, it should be red and not blue, the color only change if you click another button. It is like radio buttons but I need an <a> tag and I can't use radio buttons

HTML

<div class="text-center">
<a onclick="changeColor()" href="applications-new.php?program_id=1" id="one" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">Boot Camp I</a>
<a onclick="changeColor()" href="applications-new.php?program_id=2" id="two" name="program-2" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">Boot Camp II</a>
<a onclick="changeColor()" href="applications-new.php?program_id=3" id="three" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">Boot Camp III</a>
<a onclick="changeColor()" href="applications-new.php?program_id=4" id="four" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">LSPP I</a>
<a onclick="changeColor()" href="applications-new.php?program_id=5" id="five" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">LSAT</a>

JS

function changeColor(){

    if(document.getElementById('one').clicked === true)
     {
       document.getElementById('one').style.background = "red";
     }else {
       document.getElementById('one').style.background = "#012D6B";
     }

     if(document.getElementById('two').clicked === true)
     {
       document.getElementById('two').style.background = "red";
     }else {
       document.getElementById('two').style.background = "#012D6B";
     }

     if(document.getElementById('three').clicked === true)
     {
       document.getElementById('three').style.background = "red";
     }else {
       document.getElementById('three').style.background = "#012D6B";
     }

     if(document.getElementById('four').clicked === true)
     {
       document.getElementById('four').style.background = "red";
     }else {
       document.getElementById('four').style.background = "#012D6B";
     }

     if(document.getElementById('five').clicked === true)
     {
       document.getElementById('five').style.background = "red";
     }else {
       document.getElementById('five').style.background = "#012D6B";
     }
}

My js doesn't work. Also, I believe that there is a better way to do this without 5 if and 5 else statement

PS: this is my css just in case:

.bkgrnd-dkblue {
background-color: #012D6B;
color: #FFF;
}

 .btn-width{
  width:130px;

}

 .change-btn-color:hover {

   background-color: #9DC9BA;
  }

Thank you

rblarsen

Create a class that makes the background red, and then toggle the class on the element clicked. When you click the button the first time, it will add the class, and the background will change. When you click it again, it will remove the class and turn the background-color "normal" again.

HTML:

<div class="text-center">
<a onclick="changeColor(this)" href="applications-new.php?program_id=1" id="one" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">Boot Camp I</a>
<a onclick="changeColor(this)" href="applications-new.php?program_id=2" id="two" name="program-2" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">Boot Camp II</a>
<a onclick="changeColor(this)" href="applications-new.php?program_id=3" id="three" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">Boot Camp III</a>
<a onclick="changeColor(this)" href="applications-new.php?program_id=4" id="four" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">LSPP I</a>
<a onclick="changeColor(this)" href="applications-new.php?program_id=5" id="five" name="program-1" class="btn btn-md bkgrnd-dkblue btn-width change-btn-color">LSAT</a>

JS:

function changeColor(el){
    jQuery(el).toggleClass('red');
}

CSS:

.red {
    background-color:red;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change the background color and text color with the click of a button in javascript?

From Dev

How to change the color of a button?

From Dev

How to change button color

From Dev

How to change the color of ttk button

From Dev

How to change color text in button?

From Dev

How to change color of CompoundDrawable on Button?

From Dev

How to change the background color of a button?

From Dev

How to change color text in button?

From Dev

JavaScript - If radio button is checked, change background color

From Dev

Display the value of button in the text box and change the color of the selected button in javascript

From Java

How to change the text color of the button theme in Flutter

From Java

How to change the appBar back button color

From Dev

how to change the wordpress color picker button text?

From Dev

How to change back button color in nav bar?

From Java

How to change color of Toolbar back button in Android?

From Java

How to change the text color of a Vuetify button?

From Dev

How to change the jquery ui dialog button color?

From Dev

How to change text background color of a button in Android?

From Dev

How to change text color in button hover property

From Dev

How to change the tint color of the clear button on a UITextField

From Dev

How to change Kivy AccorditionItem button's color?

From Dev

How to change color button after clicking on it

From Dev

How to change background color of button on press in kivy?

From Dev

How to change the text color of Radio Button in Android?

From Dev

How to Change the UIAlertAction button Color in iOS?

From Dev

How to change spinner button color in Android?

From Dev

How to change color of Bootstrap disabled button?

From Dev

How to change indicator color in Toggle Button ( Android)

From Dev

Android how to change the button color by the text

Related Related

  1. 1

    How to change the background color and text color with the click of a button in javascript?

  2. 2

    How to change the color of a button?

  3. 3

    How to change button color

  4. 4

    How to change the color of ttk button

  5. 5

    How to change color text in button?

  6. 6

    How to change color of CompoundDrawable on Button?

  7. 7

    How to change the background color of a button?

  8. 8

    How to change color text in button?

  9. 9

    JavaScript - If radio button is checked, change background color

  10. 10

    Display the value of button in the text box and change the color of the selected button in javascript

  11. 11

    How to change the text color of the button theme in Flutter

  12. 12

    How to change the appBar back button color

  13. 13

    how to change the wordpress color picker button text?

  14. 14

    How to change back button color in nav bar?

  15. 15

    How to change color of Toolbar back button in Android?

  16. 16

    How to change the text color of a Vuetify button?

  17. 17

    How to change the jquery ui dialog button color?

  18. 18

    How to change text background color of a button in Android?

  19. 19

    How to change text color in button hover property

  20. 20

    How to change the tint color of the clear button on a UITextField

  21. 21

    How to change Kivy AccorditionItem button's color?

  22. 22

    How to change color button after clicking on it

  23. 23

    How to change background color of button on press in kivy?

  24. 24

    How to change the text color of Radio Button in Android?

  25. 25

    How to Change the UIAlertAction button Color in iOS?

  26. 26

    How to change spinner button color in Android?

  27. 27

    How to change color of Bootstrap disabled button?

  28. 28

    How to change indicator color in Toggle Button ( Android)

  29. 29

    Android how to change the button color by the text

HotTag

Archive