0

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);"/>
Kunj
  • 1,980
  • 2
  • 22
  • 34
VIJU
  • 17
  • 2
  • 2
  • 7

6 Answers6

10

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

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

JS 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;
}
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
1

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>

DEMO

Girish
  • 11,907
  • 3
  • 34
  • 51
0

Why not use css for the background color? http://jsfiddle.net/

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); 
}
Marcel
  • 375
  • 1
  • 8
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)";
    }
});
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");
});
boomchickawawa
  • 508
  • 1
  • 6
  • 25