Skip to content Skip to sidebar Skip to footer

Background Color Change From Dropdown Using Javascript

I would like to be able to change background colour by having to choose from drop down menu.

Choose a colour to change

Solution 1:

Here is a solution using jQuery

$(document).ready(function () {

//$("#background").css("background-color",$.cookie("defaultColor"));

    $("#background-change").change(function (event) {
      var color =  $(this).val();
       $("#background").css("background-color",color);

        //$.cookie("defaultColor",color);
    });
});

The code will change the background based on the selected value in the dropdown list.

To set and retrieve cookie using jQuery, you have to use the jQuery Cookie Plugin

Use this code to set cookie

$.cookie("defaultColor",color);

Then use this code to retrieve cookie and set it as the backround color

$("#background").css("background-color",$.cookie("defaultColor"));

Check the Fiddle


Post a Comment for "Background Color Change From Dropdown Using Javascript"