Using jQuery and CSS to change to "loading" status

My current setup includes the following:

<div class="click red"></div>
<div class="click green"></div>
<div class="click blue"></div>

When the "click" is clicked, I want to add the class "load" to the div. I have successfully achieved this, but the "red," "green," and "blue" classes all have background images set, so I need to remove those classes. While I know how to do this in a sloppy way by extracting the class for the div "click blue" (as returned by jQuery), stripping out what I need, and replacing it, I believe there must be a more proper method.

What is the correct approach to accomplish this task? I need to remove the second class from the div and replace it with the class "load." Is there a specific method to call each class individually, something like .attr("class")[2]?

Answer №1

If you are aware that the class is "click", you have the option to simply assign the class, rather than constantly adding and removing it. Here's how:

$('div').attr('class', 'click load');

If you are certain about the classes (assuming this is a basic example) you wish to eliminate, you can proceed as follows:

$("div").removeClass('red green blue').addClass('load');

Edit: After receiving feedback - You can save the class using $.data() and retrieve it later to revert back after loading. This can be done in the following way:

Within your click function:

$.data(this, 'class', $(this).attr('class')); //save the original class
$(this).attr('class', 'click load');          //assign the loading class

To restore it later:

$(this).attr('class', $.data(this, 'class')); //restore original class

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

Is there a way to stop the execution of myFunc after it has been performed 5 times using clearInterval?

This code is designed to notify online shoppers that they need to choose a color from a dropdown menu before adding an item to their shopping cart. If no color is selected, the text will blink to alert them. How can I modify this code so that the blinking ...

There seems to be a clash between Modal Semantic UI React and Bootstrap causing issues

I created a project using .net core MVC. I have integrated the semantic-ui-react modal by importing its library and linking the CSS for it to function properly. However, I encountered an issue where the bootstrap CSS was conflicting with the CDN for semant ...

What is the best way to preserve drop down selections on a page containing numerous dynamically named drop down lists in asp.net mvc 2?

As I was creating this question, I read through the provided links but realized that what I'm attempting to do is slightly different as I am using ASP.NET MVC 2. I apologize in advance for the lengthy explanation. To summarize my question concisely: ...

Customize the underline color of Material-UI's Input component

Trying to create an input component with a white underline. However, the underline color changes to black when hovering over it. It should remain white. Override the underline class as shown in the demo and instructions below. Despite trying to implement t ...

swap out a JavaScript function for a fresh function

In our ordering system, there is a hidden function that I cannot access. Interestingly, this function contains a spelling error which causes a grammar issue to appear in a popup when a user interacts with it. Is there any way for me to either modify the t ...

Utilize a class within a Framer Motion element to incorporate animations or set initial properties

Is there a way to apply a class using Framer Motion tag in the animate and initial props? Here's an example: <motion.div initial={{ className: 'hidden' }} animate={{ className: 'visible' }} > <div>yo</div> & ...

What is the best way to rearrange a sidebar column to be positioned between central content columns on a mobile

I am looking to design a layout with the following components: Left Sidebar Main Content Right Sidebar Extra Content My goal is to achieve the following: On larger screens: Ensure that the "Extra Content" aligns directly below the main content with the ...

Determining the exact position of an absolute element within a Bootstrap container

In my design, I am using a full-width cover image with a bootstrap container class containing an absolutely positioned image on top of the cover image. My goal is to have this image positioned exclusively on the right-hand side of the container. Below is ...

The radio button's dynamically created OnClick event is unresponsive

I have a dropdown list and two textboxes that I want to dynamically append to a table row, along with a radio button as the third column, when a button is clicked. The code below is functioning, however, I am experiencing issues with the onclick event of ...

Nested loops combined with a timeout occasionally results in failure

I encountered a problem with the loops and timeouts in my script that I created for practice. If you want to take a look at the script, you can find it here: http://codepen.io/JulienBarreira/pen/EWNoxJ When running the script, I noticed that sometimes one ...

Guide the user to a specific website and replicate the user's action of pressing the down arrow or clicking a button three consecutive times

Currently, I am facing an issue with a WordPress slider that lacks anchors for linking to specific sections. I am wondering if there is a way to direct a user to a URL and simulate the action of pressing the down arrow or clicking a button multiple times ...

Is it common for functions to be outlined in standard CSS practices?

I am curious if the CSS standard includes definitions for "functions"; I have not come across any information about them on w3schools or other sources. When I refer to "functions," I am talking about calls such as rgb(int, int, int) or url(string) that ar ...

Header unresponsive to user interactions

Hey everyone! I'm new to using Foundation and I've hit a roadblock in my code on a medium-sized device. I would greatly appreciate your input to help me identify my mistake. Below is the snippet of my code: <div class="row"> <div cl ...

The print preview displays a single div in an incorrect location

I have successfully created a javascript plot and wanted to incorporate a legend that overlaps the plot. Unfortunately, the function I used does not support directly overlapping legends, but it does allow for placing the legend in a separate div. To work a ...

How can I display the most recent offcanvas opening at the top of the page?

The issue I'm facing is related to the offcanvas3 opening behind offcanvas2. It appears like this: $("#open-offcanvas2").on("click", function(){ $("#offcanvas2").offcanvas("show") }) $("#open-offcanvas1").on("click", function(){ $("#offcanvas1" ...

Is there a way to display a sub-menu while scrolling through a sub menu?

My dropdown menu is not displaying properly when there is a scroll bar on the page. I am using Bootstrap for my website. https://i.sstatic.net/tZFXh.png Below is the HTML code I am using: <a tabindex="-1" href="#">Location</a> ...

What is the proper method for extracting XML data in PHP that was transmitted to the server in the format of text/xml?

I am currently struggling with a client-side script written in jQuery that is sending text/xml data to the server. The issue I am facing is how to properly parse this request because the data sent is not in the form of a query string variable. Here is an e ...

Tips for sending a file array together with other string/int variables from JavaScript to PHP

I am looking to pass the imageArray[] along with a variable trackNo. Passing the imageArray[] is not an issue, but I am unsure of how to include other variables with the FormData(). When researching online, I only came across examples of people passing a ...

Preserve scripts when loading content into a pjax container

I am currently working on my WordPress site and incorporating pjax for the first time. My goal is to update both the #main container and a separate div that houses a stretched background slider. The challenge I encountered was with the script tag associate ...

Expanding content based on height using React

I have successfully implemented the show more/show less feature. However, my issue is that I would like it to be based on the number of lines or screen height rather than the number of characters. This is because the current setup may look awkward on certa ...