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
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.
Comments