NadanSHA NadanSHA - 2 months ago 16
CSS Question

If/else statement - make "else" same as before state

I have certain -select- option on my future web page where costumers have option to select between 2 values (value white and value black). These options change how some content look. Value white is default option and i don't need to change there anything when user chooses that one. Value black is additonal one and when customer selects it, some visual things change. I do those changes using jquery and statemants if/else (if value is selected do that or else...).

My question now is, is there any easy way to make else statment "delete" all those changes from "if" statement and return everything to default state, without much writing same things over and over again?

Example of code (this is just simple example tho):

$("#optioncolor").change(function(){
if ($("#optioncolor").val() == "#262626") {
$("#row").css("background-color", "rgba(0, 0, 0, 0.8)");
$("#botbor").css("background", "white");
} else {
???
}
});


With else, i want do delete entire if statement!

I hope you understand.
Thanks.

Answer Source

As Chetan V already pointed out, use removeAttr('style') to clear any previously defined styles on the elements.

$("#optioncolor").change(function(){
    if ($("#optioncolor").val() == "#262626") {
        $("#row").css("background-color", "rgba(0, 0, 0, 0.8)");
        $("#botbor").css("background", "white");
    } else {
        $("#row").removeAttr('style')
        $("#botbor").removeAttr('style')
    }
});