0

I have invoked some code in my HTML that puts a button in a state of having a background of white when it's clicked.

How do I click on the button and revert back to its original position? This is what I have so far in my code:

$("#button").click(function(){
    $("#button").css("background", "white");
});

What code should I put here to have it back to original form when clicked?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sam Opoku
  • 69
  • 2
  • 8
  • possible duplicate of [How can I "reset"
    to its original state after it has been modified by JavaScript?](http://stackoverflow.com/questions/5557641/how-can-i-reset-div-to-its-original-state-after-it-has-been-modified-by-java)
    – TylerH Sep 28 '15 at 01:24
  • That has completely nothing to do with anything i asked buddy – Sam Opoku Sep 28 '15 at 01:26
  • It's exactly what you're asking buddy; how to return an element back to its original state. – TylerH Sep 28 '15 at 05:15

4 Answers4

0
$("#button").click(function(){
    var button = $("#button");
    if (button.css("background") === "white") {
        button.css("background", "black");
    } else {
        button.css("background", "white");
    }
});
dursk
  • 4,435
  • 2
  • 19
  • 30
0

It is generally simpler toggling a class on the button and using css rules to manage the style differences:

$(function(){
    $("#button").click(function(){
        $(this).toggleClass( "white_bg");
    });
});

CSS

#button.white_bg{
   background:white;
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

Use a toggle function

$( "p" ).click(function() {
  $( this ).toggleClass( "highlight" );
});

.highlight {
background: yellow;
}

from:

http://api.jquery.com/toggleclass/

NutellaAddict
  • 574
  • 6
  • 24
0
$(document).ready(function(){

  original_background = $("#button").css("background");
  $("#button").click(function(){
     if ($("#button").css("background")==original_background) {
        $("#button").css("background", "white");
     } else {
        $("#button").css("background", original_background);
     }
  });

})
Yokowasis
  • 363
  • 2
  • 11