How to change the background color of select options using JQuery

My webpage includes multiple select boxes with different options:

<select name="item-0-status" id="id_item-0-status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>

Since these select boxes are auto-generated in Django, I cannot directly apply CSS classes, IDs or attributes to the options. Instead, each select element has its own ID like 'item-0-status', 'item-1-status', 'item-2-status', and so on.

How can I style the options with CSS colors using the ID of the select element?

I've attempted to use the code from How do you select a particular option in a SELECT element in jQuery? to set the 'offline' options to red:

$('select[id$=-status][id^=id_item-]').children().find('option[value="2"]').css('background-color','#FF0000');

Unfortunately, this code doesn't seem to have any effect. How can I get this to work properly? While I understand that browser support may vary, I am specifically focusing on making it work in Firefox 3.6 for this question.

Answer №1

Something similar should be effective, but please keep in mind that I am not completely certain.

$(function(){   

$('#id_item-0-status').children().each(
        function (){
            if($(this).val() == 2){
                $(this).css({'background':'red'});
            }
        }
);

})  

Answer №2

There seems to be a typographical error.

The correct syntax is background-color. You appear to have omitted the letter 'r'.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Load different select options using jQuery when the selection is changed

Below the select box with the id "course_name" and a div with an id of "dates", you will find my Javascript code. <script> $('#course_name').change(function() { url = "date_range.php?courseID=".$('#course_name').val(); $("# ...

Leverage the power of Angular to seamlessly integrate jQuery code across your

Interested in incorporating the plugin into my AngularJS project. To achieve this, I have to: $(document).ready( function() { $("html").niceScroll(); } ); The challenge is figuring out where to place this code so that it runs site-wide and ...

Altering the color of a div based on a specified value condition

I am in need of assistance with a div structure similar to this: <div id="namacoc" tabindex="1" class="filled-text" placeholder="Input your name" contenteditable ></div> <div id="position" tabindex="1" class="filled-text" placeholder="Input ...

I'm puzzled as to why the '>' combinator is not functioning the way I anticipated. It seems I must now style elements individually instead of being able to style them together as I originally thought

Check out the fiddle and code snippet here: http://jsfiddle.net/ogr27cLa/4/ <div> <ul id="navigation"> <a href="http://www.google.com"><li> <p>one</p> <ul> <a href="http://www.bi ...

Implementing pagination within a PHP foreach loop

I have a code snippet that fetches films from the database and organizes them by year, then displays each film for that particular year. However, my list of results is becoming too lengthy and I need to implement pagination. I haven't been able to fin ...

Having trouble getting padding to work inside a pre block, even when using a wrapper div?

Snippet of HTML Code: <div class="prewrap"> <pre> stepsize = .01 samplestimes = 30 universex = seq(-1, 1, stepsize) universey = sin(pi * universex) </pre> </div> Sample CSS Styles: #prewrap { background-color: #e3e3e3; pa ...

Could my mental model be flawed? When a page is accessed using https, a relative css path will be invoked using the same protocol

When your page is accessed using the https protocol, any relative path to an external CSS file will also be called using the https protocol. Do you really need to encrypt/decrypt CSS content? :D However, if you use an absolute path to reference an external ...

Streamline the process of implementing a sticky menu scroll function with jQuery

I have a horizontal navigation on my single page website that turns into a sticky navigation at a specific point. I have created a scroll-to navigation that functions like this: $(document).ready(function () { $("#button0").click(function() { ...

Creating a sophisticated form design with multiple input fields using DIV elements in HTML

I am looking to create a multi-column form layout that allows each row to have a different number of fields. My initial approach was using tables and the colspan attribute within td tags for layout structure. However, I have learned that using tables for l ...

Tips for eliminating the space within the '<td>' border tags

I am attempting to replicate the first table, but I keep getting results similar to the second table instead. My approach involved creating a table and filling the <td> tags with labels. However, after setting the borders to dark, there are unwanted ...

Combining jQuery, Superfish, Supersubs, Prototype, and IE results in a major setback

In order to implement a vertical menu on certain pages, I have incorporated the Superfish menu plugin for jQuery. To ensure that each level within the menu system is as wide as the widest LI element, I am also utilizing the Supersubs.js plugin. After addi ...

Utilizing CSS to incorporate percentage points

Currently, I am facing a challenge in implementing a vertical line with percentage marks. It needs to be positioned relative to the percentage bar, just like how it appears in the screenshot below: https://i.stack.imgur.com/CNWUV.png I attempted a basic ...

Place objects at the bottom of the container

Is there a way to align each "Read more" button at the bottom of its respective div without setting a specific height for the divs or changing any markup or style elements? While pseudo class selectors could be used, is there a simpler approach that would ...

Issue with Swiper JS: The buttons on pages three and beyond are unresponsive

I'm having trouble with the functionality of the "Back" button on pages 2 and 3 of my website. Can anyone help me figure out why it's not working? My goal is to create a website that resembles a game menu, similar to Deus Ex The Fall. I'm u ...

What steps are involved in merging Webflow code with an Angular web application?

As a newcomer to web development with Angular, I have the source code of a website already developed with Angular and I want to customize it to suit my needs. My issue is that when I add generated code from Webflow to an existing partial page, the browser ...

Struggling to forward JSON data via AJAX to Django without relying on jQuery

My goal is to utilize AJAX in conjunction with Django, but I want to achieve this without relying on jQuery. So far, I have made progress in sending URL encoded data from AJAX to Django successfully. However, I am currently facing an issue where I am unabl ...

Discovering elements with Selenium through XPATH or CSS Selector

Having trouble locating the "import" element using Selenium Webdriver in C#. I've attempted the following codes but haven't had any luck: driver.FindElement(By.XPath("//*[@class='menu_bg']/ul/li[3]")).Click(); driver.FindElement(By.XPa ...

Refreshing Flask Jinja2 template table with a button press

I am utilizing a Flask app for the automated provisioning of servers on my cloud platform, and one of the features allows users to upload a spreadsheet that is then parsed into an interactive HTML table. After populating the table with data, the fields ar ...

Is it possible to conceal and completely empty the TextBox once the checkbox is deselected?

When the checkbox is checked, the textbox is displayed; otherwise, it remains hidden. However, the value is not being cleared. Can someone please help me with this issue? Thank you in advance. HTML <div class="munna"> <in ...

Two sections that are supposed to have the same height, but one seems to be slightly taller than the other

Upon closer inspection: At first glance, they may appear identical at certain zoom levels. However, a more detailed examination through zooming in and out reveals a noticeable discrepancy. Firefox's default zoom may make them seem incorrect, while Chr ...