Having issues changing the color of fontawesome icons

I am struggling to change the color of a fontawesome icon when it is clicked.

 <a id="thumbsup">@Html.FontAwesome(FontAwesomeIconSet.ThumbsUp, FontAwesomeStyles.Large2x)</a>

My goal is to have the icon start off as gray, turn blue when clicked, and then return to gray when clicked again (the original color of the icon is black). Is there a way to switch between these two colors on a fontawesome icon other than its default color?

When I attempt to toggle between the original black color and blue using toggleclass in jQuery, it works properly.

 //jquery
 $("#thumbsup").toggleclass("bluecolor");
 //css
 .bluecolor{
 color:#0026ff;
 }

However, if I try to set the initial color as gray by adding a class in the HTML, the icon appears gray but does not toggle to blue when clicked with jQuery.

//html
<a id="thumbsup" class="graycolor">@Html.FontAwesome(FontAwesomeIconSet.ThumbsUp, FontAwesomeStyles.Large2x)</a>
//css
.graycolor{
color:#999;
 }
//jquery
$("#thumbsup").toggleclass("bluecolor");

When I apply this logic to plain text, the toggling works perfectly. Could it be that it doesn't work on fontawesome icons because they are loaded as classes? Is there another method to achieve this effect?

Answer №1

Make sure to include both classes when using toggleClass:

$("#thumbsup").toggleClass("bluecolor graycolor");

Answer №2

Remember, it's toggleClass, not toggleclass. Experience it for yourself by running the code snippet below and clicking on the link.

$("#thumbsup").click(function(e){
    $("#thumbsup").toggleClass("bluecolor");
});
.graycolor{
    color:#999;
 }
.bluecolor{
    color:#0026ff;
 }
<a href="#" id="thumbsup" class="graycolor"><i class="fa fa-2x fa-thumbs-up"></i> Thumbs Up!</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

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

Having trouble with jQuery variable assignments not working in Safari?

My jQuery 1.3.2 code is encountering issues with Safari 4 for reasons unknown to me. Even though all my javascript references are placed right before the closing <body> tag, I am facing difficulties with the following snippet: var status = $(' ...

When interacting with elements with the "user-select: none" property, WkWebView still allows text selection

When using an iOS iPad app with a single fullscreen WKWebView, an issue arises when long-pressing on an area with user-select:none - the first text in the next available area without user-select:none gets selected instead of being ignored. Is there a way t ...

Tips for using an AJAX request to upload a CSV file using Flask

Currently, I am in the process of updating an older project that relies on AJAX. To facilitate this update, I have chosen to integrate Flask into the system. The specific page I am focusing on at the moment requires the capability to upload a CSV file and ...

What does the term "root element" refer to in the Material-UI documentation?

I am finding the terminology used in material ui confusing and would appreciate if someone could clarify it for me. For example, let's consider this information from https://material-ui.com/guides/api/: Spread Undocumented properties supplied ar ...

Plugin for managing responses other than 200, 301, or 302 in forms

Currently, I am using the jQuery Form Plugin in my project. Everything works smoothly when the server sends a 200 response as it triggers the success listener seamlessly. However, according to the standard protocol, the browser automatically handles 301 ...

Repairing a syntax error in a jQuery selector variable

$(".className").click(function(){ var link = $(this).find("a").attr('href'); //output is '#myID' var findItems = $(link '.mydiv').length; //WRONG var findItems = $(link + '.mydiv').length; ...

Floating action buttons in CSS

I'm trying to create a button that reveals additional options when hovered over. I've been looking for a method similar to what's shown in the image below, but haven't been successful. This needs to be implemented on a web page using HT ...

Exploring methods for comparing AJAX and PHP values within an AJAX success function

const salesId = $('#assign_sales_id').val(); $('#'+id).children('td[data-target=sales_id]').text(salesId); I am interested in verifying if the selected name from users matches the ID: id = '$('#assign_sales_id' ...

Utilizing CSS nested spans and setting custom width attributes

I'm curious about how nested spans and CSS handle the width attribute. In the HTML code provided, the 'wide' class sets the width while the 'box' class adds a border. I've noticed that the width is only applied when both class ...

The division element shifts to the left upon invoking the slideUp() function

I am currently working on a page that contains a form with two different sections - one is visible and the other is hidden. When the user clicks a button in the first section, it slides up using slideUp() while the hidden section slides down using slideDow ...

Tips for adjusting column sizes in react-mui's DataGrid based on screen size

I would like the title column to occupy 3/4 of the full row width and the amount column to take up 1/4 of the full row width on all screens sizes (md, sx...). Here is the updated code snippet: import React from 'react' const MyComponent = () =&g ...

Learn the process of transferring information through ajax while managing dependent drop-down menus

I have successfully set the initial value from the first combo-box and now I am looking to send the second variable from the second combo-box and receive it in the same PHP file. Below is the Ajax code snippet: $(document).ready(function(){ $(".rutas") ...

Using jQuery to detect and respond to changes in an asp:RadioButtonList

Upon page load and when a user selects a radio button, the code below focuses on a textbox. However, I am looking to modify it so that if the last radio option "Mail" is chosen, the focus shifts to the btnSubmit control instead of txtPayerName. $(funct ...

Scrolling the bottom of a PDF object element in HTML using JavaScript - a simple tutorial

There is a Pdf object displayed on my website <object data="data/test.pdf" type="application/pdf" width="700" height="900"> </object> I'm trying to automatically scroll this pdf to the last page when the page loads. I've found tha ...

What is the best way to adjust the z-index when hovering over an li element

In my <ul>, each <li> contains an image. I want these images to scale and become opaque upon hover. However, the issue is that when they scale, they get covered by the following image. Unsure if the z-index is correctly placed... Below is the C ...

Dealing with errors when Ajax response is not defined

Trying to display errors when Ajax is unsuccessful, but struggling to access the specific error details. example Here is the information from the network tab playground {"message":"The given data was invalid.","errors":{"title":["The title field is requ ...

Sending form data without interrupting the user interface by using asynchronous submission

Using jQuery version 1.7.2, I am currently using the submit() method to send a form via POST. This triggers a Python cgi-bin script that performs some tasks which may take around ten seconds to finish. Upon completion of the task, the Python script instruc ...

After removing a record from the data table, the deletion does not take effect until the page is reloaded. Is there a way to achieve

I want to remove a record from a datatable without having to reload all the pages: $(function () { $(".btndeletesoftware").click(function () { $.ajax({ type: "POST", url: '@Url.Action("Delete")', ...

What is the best way to keep the heights of two divs in sync?

Is there a way to make two divs have the same height, even though their content determines their individual heights? I am looking for a solution that doesn't involve using a table or a parent div. I'm new to JavaScript, so I'm hoping there i ...

Restrict the width of CSS columns to the value of an assigned variable

I'm struggling with centering a one-row table within a div. Specifically, I'm having trouble adjusting the size of two columns to match the length of the content inside them (team name and round). The HTML code is: <div id="greeting"> ...