Wednesday 31 May 2017

javascript - Change a style then with second click reset to original class





I have a button and it takes some styles with first click. But I want the user to be able to reset to the original with second click on own button or any button in page.



How can I do this?







my codes





$("#checkButton").click(function() {
$("#checkButton").css("color", "yellow");
});










Answer



You can do it with CSS styling, but I've made a below snippet in case if you want to change color without make CSS.



Check current color, and then set the different one.






$("#checkButton").click(function() {
var color = $("#checkButton").css("color")
var yellow = "rgb(255, 255, 0)"

if (color !== yellow) {
$("#checkButton").css("color", yellow);
} else {
$("#checkButton").css("color", "");
}

});






No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...