0

I am trying to change the background-color depending on the value that is returned by the selected value. I have the following code but I am not getting anywhere:

<script>
$(document).ready(function() {
    $('#assessments').change(function(){
        if ($(this).val() == "0") {
            $('select').css('background-color', '#FF0000');
        } else if ($(this).val() == "1") {
            $('select').css('background-color', '#00FF00');
        } else if ($(this).val() == "2") {
            $('select').css('background-color', '#0000FF');
        }
    });​
});​
</script>

<select id='assessments'>
    <option value='0'>NYC</option>
    <option value='1'>C</option>
    <option value='2'>G</option>
</select>

EDIT: Alrighty besides the DOM ready handler not being there (which I have now added on) the select background-color is still not changing. Am I fetching the value wrong from the select box? Is there a way to output the value so I can check that it is indeed getting the right value or any value at all.

3
  • @isherwood I have included the jQuery library in my project. Commented Mar 11, 2014 at 1:21
  • is it necessary to use jQuery... CSS solution could be much simpler Commented Mar 11, 2014 at 1:22
  • 1
    Feel free to put a CSS solution together. I'm not sure how that's possible. Commented Mar 11, 2014 at 1:43

1 Answer 1

5

You'll need a DOM ready handler with that, as the script appears before the elements in the DOM.
Also, you'll have to include jQuery to use jQuery methods.

<script>
$(function() {

    $('#assessments').change(function(){
        if ($(this).val() == "0") {
            $('select').css('background-color', '#FF0000');
        } else if ($(this).val() == "1") {
            $('select').css('background-color', '#00FF00');
        } else if ($(this).val() == "2") {
            $('select').css('background-color', '#0000FF');
        }
    });​

});

</script>

<select id='assessments'>
    <option value='0' selected>NYC</option>
    <option value='1'>C</option>
    <option value='2'>G</option>
</select>

Another option would be to move the script to below the elements in the DOM

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

6 Comments

Yup. Can't act on elements that don't exist yet.
Why do so many programmers forget this? Isn't it the first thing mentioned in every jQuery tutorial?
@Barmar - Yes, it's the first thing in every book, and on the jQuery site, but many just copy examples from Stack Overflow where we tend to not include the DOM ready handler in most cases, and they see it working on jsFiddle where it's automatically included, and they of course didn't bother reading the manual, so they have to ask.
Apart from this thing being mentioned in every jQuery tutorial, it is also mentioned in every third question with jquery tag here. Or better to say, this is solution to every third question. Just sad.
There must be something else that I am doing wrong because I have added the ready handler but the background-color is not updating still. Any thoughts?
|

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.