Modify the class of the <select> element when it has been clicked or altered using JavaScript or jQuery

There is a select element styled as a dropdown list using bootstrap-select. I am interested in adding a specific class with CSS when the select is clicked or changed.

https://i.sstatic.net/fbdK4.png

  <select required class="form-control selectpicker" title="shipping">
      <option>Standard</option>
      <option>Express</option>
      <option>...</option>
  <select>

I would like to apply my class solely to this select element - changing its color to green if there is a change, and to red if it was only clicked without any modifications.

You can find the code on this link.

Answer №1

To implement the desired functionality in your JavaScript, include this code:

$('.selectpicker').on('changed.bs.select', function (e) {
   $(this).closest('.bootstrap-select').addClass("checked");
});

When the selectpicker is changed, the specified class will be added to the select box.
Remember to update your CSS with the following:

.bootstrap-select.checked > .dropdown-toggle > span {
  color: red
}

Don't forget to check out the updated fiddle here.

Answer №2

Utilize the event known as: changed.bs.select

// whenever a change occurs, this function will be executed
$('.selectpicker').on('changed.bs.select', function (e) {
 var value = $(this).val();
 // add your conditions here...
});

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

Ways to include the name of the parent element in a child element

I am facing an issue with two input elements having the same ID when trying to edit. To solve this problem, I need to add the parent div ID to the child input element. How can I append the parentId to the childId? <div class="wrapper-merge-input"> ...

Issues with integrating VUE frontend with PHP backend and API

Apologies for any language mistakes as English is not my native tongue. I hope my message is clear enough. We are in the process of developing a user system where users, upon logging in, can perform various actions such as joining events, updating their p ...

What methods are available to create distinctive, shareable links akin to those utilized by Zoom and Google Hangouts?

I'm currently developing a group video chat app and I'm facing the challenge of generating distinct shareable links for every chat room created. Can anyone guide me on how to accomplish this? My aim is for users to easily join the chat room when ...

The location of the CHROMEDRIVER for Node.js with Selenium

After trying "npm install selenium-webdriver", I am still encountering the error message below. Can anyone provide guidance on where the path is supposed to be located? Error: The ChromeDriver could not be found on the current PATH. Please download the ...

Dealing with the MethodNotAllowedHttpException issue while implementing AJAX in Laravel 5

I'm currently attempting to use ajax to upload an image, but I keep receiving the following error message: Failed to load resource: the server responded with a status of 500 (Internal Server Error). Below is my ajax code: $('document').read ...

Issue with Flask and JS: Syntax error detected - Unexpected token '<'

The Flask web app is currently working on fetching data from a database table and displaying it in HTML format using a JavaScript file that handles this operation. This whole process is integrated within a single Flask application. I've attempted to ...

What is the best way to align a button directly beside an input field using Bootstrap's columns and grid system?

Is it possible to place a button right next to an input box while utilizing the bootstrap columns/grid system? The Label is set to 2, EditorFor div to 4, and the input div to 6. When I reduce the EditorFor div to 3, it moves the input/button div closer tog ...

Controlling the visibility of HTML elements in ASP.NET

How can I control the visibility of a span element in my asp.net page without using the runat server attribute? The decision to make this span visible will be determined by the code behind, but I do not want to use runat="server". ...

Mastering the CSS Art of Working with Sprite Images and Their Positioning

I am currently working on an educational project as a front-end developer, where I aim to clone the Google homepage. To achieve this, I am utilizing Google's own sprite and here is the image of the sprite that I am using. https://i.stack.imgur.com/YT ...

To retrieve data from an AJAX response, you will receive an array in the form of a string

After requesting a list of posts submitted by users from my server, the response I received was a string containing an array of stdClass objects. If it had been an actual array, that would not have been an issue. However, it arrives as a string in the foll ...

Extracting data from tag definitions using Beautiful Soup

Here is the HTML Code: <option data-formated='&lt;span class="price"&gt;AUD $100.08&lt;/span&gt;' data-qtyid="qty-219" value="1"> Unit Price </option> ...

Workaround for Google Chrome Session Cookie Functionality

After discovering that Chrome's "Continue where I left Off" feature allows cookies and sessionStorage to persist between browser restarts, it became clear that many users were facing the same issue. Despite numerous threads on sites like Stackoverflow ...

What is the best way to exclude all elements in the array that do not match this specific

For instance, here is a snippet of code that utilizes a JSON object: "food": { "appetizers": [ { "id": 1, "image": "../image/calammari.png", "title": "rings", "price": 115 ...

Comparing $.fn.fancybox and $.fancybox: What sets them apart?

I'd like to understand the distinction between the two items shown above. In what ways do they differ from each other? ...

Is there a way for me to recreate the appearance of the outline and labeling found in Material-UI's outlined textfield?

Can anyone help me figure out how to hide the border behind the title text in an outlined textfield from Material-UI that I am trying to imitate? In the image below, you can see "Due Date/Time" taken from Material-UI library where the title hides the bord ...

Inject Angularjs Controller into Module Once DOM is Initialized: Tips and Tricks

When injecting new DOM elements with a controller, I encountered the following issue: app.controller('cart', function ($scope, $http) { $scope.content = { label: "hello, world!", }; }); var $html = '<div ng-controller="c ...

What steps do I need to take to make this JavaScript code function properly? How can I create a carbon copy email setup in

Recently, I've been struggling to implement this particular code on my website as I am not very proficient in JavaScript. Despite searching for various online resources, none of them seem to address my issue. This code is meant to be a part of a form ...

Place the button inside the form following another block element

Here is the HTML code I am working with: <div class="actions"> <input id="submit-car" name="commit" type="submit" value="Добавить объявление"> </div> and here is a fiddle example: http://jsfiddle.net/348U4/ In ...

What is the Ideal Placement for the MuiThemeProvider Component?

Greetings! I am in the process of developing a new React application and I have chosen Material Ui as my preferred CSS library. I am facing some challenges in integrating it smoothly with react-router. Specifically, I am confused about where to place the M ...

Attempting to implement CSS onto jQuery slideToggle upon the clicking of links

Need to implement a show/hide functionality for a series of lists using jQuery slideToggle. After clicking on the hyperlinks that trigger the show/hide action, I want to change the CSS style of those hyperlinks and then revert back to the original style up ...