2

Here's my jQuery, thus far:

var themes = {};
themes['blue'] = {
    'background': 'blue',
    'color': 'white',
};
themes['red'] = {
    'background': 'red',
    'color': 'green',
};

$("#colors").change(function() {
    var theme = $(this).val();
    $("body").css(themes[theme]);

Basically, I am looking to change the css based on what the user selects from the #colors dropdown menu:

<select id="colors">
    <option selected="selected">Colors:</option>
    <option value="Blue">Blue</option>
    <option value="Red">Red</option>
</select>

The following worked fine:

var themes = {
    blue: {
        'background': 'blue',
    }
};

$("#colors").change(function() {
    var color = themes['blue'];
    $("body").css(color);
});

But I want to add support for more than just blue and more than just the background. Any advice would be greatly appreciated!

1
  • In your case you have your key down as "blue" but the value coming in is "Blue" – I would convert these to the same case. You can use variables as key, just as you have tried. Commented Jun 23, 2015 at 19:40

3 Answers 3

3

In your options you used Blue and Red, but in the object the property names are lowercase.

So either use the same case everywhere, or use toLowerCase:

var theme = $(this).val().toLowerCase();
$("body").css(themes[theme]);

var themes = {
  blue: {
    background: 'blue',
    color: 'white',
  },
  red: {
    background: 'red',
    color: 'green',
  }
};
document.getElementById('colors').onchange = function() {
  var theme = this.value.toLowerCase();
  Object.assign(document.body.style, themes[theme]);
}
<select id="colors">
  <option selected="selected">Colors:</option>
  <option value="Blue">Blue</option>
  <option value="Red">Red</option>
</select>

Sign up to request clarification or add additional context in comments.

1 Comment

I question my own sanity at times. Thank you!
0

Use lower case values, because object keys are lower case.

var themes = {
    blue: {
        background: 'blue'
    },

    red: {
        background: 'red',
        color: 'white'
    }
};


$("#colors").change(function() {
    var value = $(this).val().toLowerCase();
    $('body').css(themes[value]);
});

Comments

0

With the multiple color themes support for an application, we can also follow a more robust approach like following.

Update your html and add the following in head

<link id="theme-scheme" rel="stylesheet"/>

Now, for every theme create its specific theme-{theme_name}.css, for e.g. for blue you can have theme-blue.css and for red you can theme-red.css

So, now update your function like the following

$("#colors").change(function() {
    var theme = $(this).val().toLowerCase();
    $('#theme-scheme').attr('href', '/your_base_path/theme-' + color + '.css');
});

This approach gives you flexibility to add any style on any element without having additional dependency on script for every style.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.