Here I have written a JavaScript to change color of button to green when clicked once and when I click the button again its color should change back to orange color. The default color of button is orange. I have given the rgb values of color. But, when I click the button its color changes from orange to green and when I click it again its color remains green doesn't change back to orange. please help me to fix this.
<script>
function colorchange(id)
{
var background = document.getElementById(id).style.background;
if(background = "rgb(255,145,0)")
{
document.getElementById(id).style.background = "rgb(26,255,0)";
}
if(background == "rgb(26,255,0)")
{
document.getElementById(id).style.background = "rgb(255,145,0)";
}
}
</script>
Here is the HTML code for input button
<input type="button" name="Ignore Select" value="Ignore Select" id="select" onclick="colorchange('select')" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Delete" value="Ignore Delete" id="select1" onclick="colorchange('select1');" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Insert" value="Ignore Insert" id="select2" onclick="colorchange('select2');" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Update" value="Ignore Update" id="select3" onclick="colorchange('select3');" style="background:rgb(255,145,0);"/>
<input type="button" name="Ignore Sleep" value="Ignore Sleep" id="select4" onclick="colorchange('select4');" style="background:rgb(255,145,0);"/>
6 Answers
it should be if(background == "rgb(255,145,0)")
use comparison operator == instead of assignment =
function colorchange(id) {
var background = document.getElementById(id).style.backgroundColor;
if (background == "rgb(255, 145, 0)") {
document.getElementById(id).style.background = "rgb(26,255,0)";
} else {
document.getElementById(id).style.background = "rgb(255,145,0)";
}
}
Demo: Fiddle
Since jQuery tag was used
<input type="button" name="Ignore Select" value="Ignore Select" id="select" class="select" />
<input type="button" name="Ignore Delete" value="Ignore Delete" id="select1" class="select" />
<input type="button" name="Ignore Insert" value="Ignore Insert" id="select2" class="select" />
<input type="button" name="Ignore Update" value="Ignore Update" id="select3" class="select" />
<input type="button" name="Ignore Sleep" value="Ignore Sleep" id="select4" class="select" />
then
.select {
background:rgb(255,145,0);
}
.select.highlight {
background:rgb(26, 255, 0);
}
and
jQuery(function ($) {
$('.select').click(function () {
$(this).toggleClass('highlight')
})
})
Demo: Fiddle
Jquery:
$(":button").on('click', function () {
var gValue = $(this).attr('class');
if (gValue == 'orange') {
$(this).removeClass("orange");
$(this).addClass("red");
} else {
$(this).removeClass("red");
$(this).addClass("orange");
}
});
CSS:
.red {
background-color:red;
}
.orange {
background-color:orange;
}
You should comparison operator instead of assignment operator
if(background = "rgb(255,145,0)")
to
if(background == "rgb(255,145,0)")
//
<script>
function colorchange(id)
{
var background = document.getElementById(id).style.backgroundColor;
if(background == "rgb(255, 145, 0)")
{
document.getElementById(id).style.backgroundColor = "rgb(26,255,0)";
}
if(background == "rgb(26, 255, 0)")
{
document.getElementById(id).style.backgroundColor = "rgb(255,145,0)";
}
}
</script>
Why not use css for the background color?
Html:
<input type="button" name="Ignore Select" value="Ignore Select" id="select" onclick="colorchange('select')" class="classA" />
<script>
function colorchange(id)
{
var el = document.getElementById(id);
var currentClass = el.getAttribute("class");
if(currentClass == 'classA')
{
el.setAttribute("class", "classB");
} else {
el.setAttribute("class", "classA");
}
}
</script>
CSS:
.classA {
background:rgb(255,145,0);
}
.classB {
background:rgb(26,255,0);
}
I would write it like this:
Where btn is your button
var background = document.getElementById(id).style.background;
var btn = addEventListener("click", function (){
if(background = "rgb(255,145,0)"){
document.getElementById(id).style.background = "rgb(26,255,0)";
}
else
{
document.getElementById(id).style.background = "rgb(255,145,0)";
}
});
My Approach :
var eButton = document.querySelectorAll("button");
var isPink = false;//using white and pink in this example
eButton.addEventListener("click", function(){
if(isPink){
document.body.style.background = "white";
}
else{
document.body.style.background = "pink";
}
isPink = !isPink; //switches between true and false
});
OR
Write css for the background and use the classList property
.pink{
background : pink;
}
JS
eButton.addEventListener("click", function(){
document.body.classList.toggle("pink");
});