Modifying bootstrap glyphicon color via jQuery: A step-by-step guide

In one of my li elements, I have a glyphicon-ok. Initially, I styled all glyphicon-ok elements to be green like this:

.glyphicon-ok {
    color: #41be47;
}

Here is an example of the li element:

<li class="active">
    <a href="#"  onclick="return getQuestions(this);"> 
        <span class="glyphicon glyphicon-ok"></span>
        &nbsp;&nbsp;test
        <i class="glyphicon glyphicon-remove pull-right" style="display: block;" ></i>
    </a>
</li>

Now, when that specific li element is clicked, I want to change the color of glyphicon-ok to white.

What should I include inside the

function  getQuestions(id) {
}

I attempted

function  getQuestions(id) {

$('span').find('.glyphicon-ok').css('color','#fff');
}

Answer №1

To adjust the appearance of an icon, simply modify the color property in the CSS file. One advantage of utilizing an icon font is the ability to apply font-related CSS properties. It's recommended to utilize the :focus and :active selectors for styling instead of resorting to javascript.

View Example

If you opt for using javascript, consider only adding or removing a class rather than directly applying styles through it.

Answer №2

If you're interested in implementing this via javascript:

The first option involves detecting a click on the icon

jQuery(document).ready(function($){
    $('.glyphicon-ok').on("click", function(){
        $(this).css("color", "#FFF000");
    });
});

See a demonstration here: http://jsfiddle.net/T9Dk9/1/

The second option detects clicks on the li element: Add a class to the li (not necessary, but helpful in larger projects):

<li class="active clickable">

jQuery(document).ready(function($){
    $('.clickable').on("click", function(){
        $(this).find('.glyphicon').css("color", "#FFF000");
    });
});

http://jsfiddle.net/T9Dk9/2/

However, there may be a method in Bootstrap to define a specific color for clicked links

Answer №3

Give this a shot:

$('[element]').css({
    background-color:'#000 !important',
});

Check out the live demonstration here

Answer №4

Hey everyone, it's time to switch things up a bit,

If you prefer the icon to turn white after clicking, just add a new class.

Give it a shot (maybe on hover):

$('span').hover(
    function(){
        $(this).addClass("white");
    },
    function(){
        $(this).removeClass("white");
    }
});

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

The error "this expression cannot be constructed" occurs when selecting dynamic properties in TypeScript

Initially, I have the following code snippet where I aim to dynamically choose a network in a straightforward manner. import * as Ethereum from '@multiplechain/ethereum/node' import * as Bitcoin from '@multiplechain/bitcoin/node' import ...

Adjustments in Spacing for Text with Significant Digits

It has come to my attention that in the United States, large numbers are often formatted with a comma between thousands, like so: $1,000,000.00. Here in South Africa, using a comma as a thousands separator is not standard practice, as it is more commonly u ...

Obtain JSON information from PHP

update/operations.php header('Content-Type: application/json'); include('../../lib/mysql.class.php'); $db = new MySQL(); if($db->Error()) $db->Kill(); //FORM VARIABLES $id = intval($_POST['id']); $sql = "SELECT * FRO ...

JustGage error: Unable to locate element with ID 0 in AngularJS

I developed a custom directive for the JustGage plugin, here is how it looks: function justGage() { return { restrict: 'E', replace: true, scope: { universalId: '@', ...

Encountered a glitch while trying to install React JS using npx create-react-app xyz

After entering the command in the terminal, I encountered an error stating: npm Err! code-ENOENT npm Err! syscall lstat npm Err! path Interestingly, this same command worked perfectly on my instructor's laptops. For reference, I have attached a snaps ...

Issue: [$injector:unpr] The provider "someProvider" is not recognized by the system and is causing an error

When using angularjs I made sure to register a service in the services directory under modules -> module_name angular.module('module_name').factory('service_name', [ function() { // Public API console.log('hell ...

Loading CSS dynamically at runtime can be seen as a mix of both success and failure

I have been attempting to optimize my app by loading fonts on demand. By retrieving fonts from a project directory generated by the app, it is able to gather all necessary data. My primary concern is whether there is an equivalent of 'app-storage://& ...

Prevent certain elements in Bootstrap navbar from collapsing

I am currently working on designing a Bootstrap navbar that features the navigation items on the left side and the social/contact icons on the right. My goal is to have the nav items collapse when needed, but have the social/contact items remain visible fo ...

Utilizing a haml document

Looking to view a haml file from a Rails app in the browser. How can this be achieved? I have already initiated rails s and accessed it through http://localhost:3000 But how can I view the haml file alone? ...

Prioritize loading one script over the other in Next.js/React

In the head section of my webpage, I have included two scripts to load: <Head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="/static/js/myscript.min.js" ...

implementing a search filter using a search bar with JavaScript, inside a Laravel Blade or HTML file

Our current website is powered by a Laravel blade template, showcasing furniture groups with multiple pieces of furniture in each group. The page is constructed using Laravel's foreach loops for both furniture groups generated by $orderformdata->pg ...

What is the best way to rearrange multiple items in an array?

I have a list of students organized by room, with students grouped within each room. I am looking to rearrange the order of student groups within a room by clicking "up" or "down". I have made an attempt below, but it seems messy and does not work properl ...

Leverage the power of personalized SCSS styles in combination with Bootstrap-Vue, Webpack, and

I have been working on an application using VueJS with Bootstrap-Vue and I encountered a challenge while trying to import a SCSS file to customize the Bootstrap variables and add some custom styles. Here are the steps I have taken so far: Installed node ...

Ways to showcase Form utilizing function

I've just started learning React JS and bootstrap. I'm attempting to load a login form using a React JS class with an embedded function, but when the function is triggered on click event, it's not rendering the Form and instead displaying co ...

When running on local machine, AWS Lambda S3.getObject gives "Access Denied" error, which is not seen when running on

Currently, I am utilizing AWS Lambda along with the serverless framework to create a service that utilizes S3 for file storage. When my lambda function named "hello" is deployed on the cloud, it functions flawlessly (with an HTTP endpoint which I can inv ...

dealing with a problem with the bootstrap navigation bar concept

I’ve been struggling to align the menu and company name in a single row. Initially, I tried setting the company name followed by some spaces before adding the menu items, but it didn't work out as expected. I've been spending the last couple of ...

Matching Overload Not Found in Typescript React: Cannot assign type 'StoreData | undefined' to 'StoreData'

Encountering compilation errors in my React app when utilizing the Reducer function. The problematic script: import { StoreReducer } from "./reducer"; import { StoreData, StoreAction } from "./types"; export const dataStore: Store< ...

How is it possible to retrieve the flex-direction property value of a div using JavaScript?

While attempting to troubleshoot this issue using Windows Chrome 59, I utilized devtools to examine the properties. let element = document.getElementById("divId"); Upon checking the value of the element, I noticed that the object structure displayed: &g ...

What is the method for presenting text based on the chosen Select Option?

I attempted to achieve this using hrefs and ids, but it did not meet my requirements. This is the desired format: div.selectcountry { margin-bottom: 10px; font-family: arial; font-size: 12px; } div.countrydepartment { font-family: ...

Learn how to easily insert a marker on a map using leaflet js in vue 3 with just a simple click

Hey everyone! I need some help with a challenge I'm facing. I can't seem to get click coordinates to create a new Marker on my map. Check out the image here And another image here ...