Pressing a reset button will clear all classes from multiple objects simultaneously

Hey there! I have a question about resetting classes when a button is pressed. Here is the HTML setup I am working with:

<a class="test testclass">Test 1</a>
<a class="test testclass">Test 2</a>
<a class="test testclass">Test 3</a>

<a class="reset_all_classes">RESET ME</a>

And here is the jQuery code I am using:

$('.test').click(function(){
    $(this).toggleClass('testclass testclass-active');
})

// Reset by removing all the testclass-active on all the <a>
$('.reset_all_classes').click(function(){
    $('.test').toggleClass('testclass-active testclass');
})

When clicking a hyperlink with the test class, the testclass will change to testclass-active. However, if both Test 1 and Test 3 are selected with testclass-active, pressing the reset button will revert them back to testclass, while leaving Test 2 with testclass-active.

I'm looking for a way to keep Test 1 and Test 3 with the testclass-active when resetting, while still reverting Test 2 back to testclass. Any suggestions on how to achieve this?

Check out the JSFIDDLE HERE

Answer №1

Code Issue

Every time you click on the element, the classes are being changed. However, when attempting to reset as per your code, it is toggling instead of going back to its initial state.

Resolution

Instead of using toggleClass(), I opted for removeClass() and addClass() to return the elements to their original state.

$('.reset_all_classes').click(function(){
    $('.test').removeClass('testclass-active').addClass('testclass');
})

View Working Demo

Answer №2

$('.click-me').on('click', function() {
  $(this).toggleClass('active');
})

// Reset by removing all the active classes on all the <button>
$('.reset-button').on('click', function() {
  $('.click-me').removeClass("active");
})
.click-me,
.reset-button,
.active {
  display: block;
  width: 100px;
  padding: 5px 10px;
  background-color: lightblue;
  margin: 20px;
  cursor: pointer;
}
.active {
  background-color: green;
}
.reset-button {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="click-me">Click Me 1</button>
<button class="click-me">Click Me 2</button>
<button class="click-me">Click Me 3</button>

<button class="reset-button">RESET ME</button>

Answer №3

Check out the demo here

With a click on the element with the class "reset_all_classes", the script removes the class "testclass-active" from all elements with the class "test" and adds the class "testclass".

The function is designed to add and remove classes without toggling, offering precise control over which classes are added and removed.

Answer №4

$('.clickme').on('click', function(){
$(this).toggleClass('clicked not-clicked');
})
 
// Reset all classes by removing 'clicked' on all elements
$('.reset').on('click', function(){
$('.clickme').removeClass('clicked');
})
.clicked, .reset, .not-clicked{
  display:block;
  width:100px;
  padding:5px 10px;
  background-color:lightblue;
  margin:20px;
  cursor:pointer;
}

.clicked{
  background-color:green;
}

.reset{
  background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="clickme clicked">Click Me</div>

<button class="reset">Reset All</button>

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

Visual representation has a numerical equivalent in HTML code

How can I insert an image into my HTML code? <input id="sendButton" type="button" value=<img src="sendbutton.jpg"> /> I attempted to do this, but the website only displays it as text "img src/>" when ...

Expanding the content in Bootstrap Navbar

I am working with a bootstrap navbar and need to customize it by adding a SignOut button and a Welcome message to the menu bar. I also want to adjust the positioning of the logo and company/city name within the navbar. Here is the current setup of my navba ...

Having trouble transferring my background image from my FTP to my website

I'm having trouble setting a background image for the header on my website. It seems that nothing is loading. The hosting service I use has disabled linking images via external URLs, so I tried uploading the image to Filezilla, but that didn't wo ...

Deactivate a Specific Popup for a Specific Page

In my project, I have a file called modal.php that contains two modal windows: Login Modal Feedback Modal The Login Modal automatically pops up every 5 seconds for users who are not logged in. The Feedback Modal opens when clicked. On my website, I hav ...

Adjust fancybox height using jQuery

I am working on a project where I need to display a fancybox containing an iframe from another domain. The iframe has dynamic content and its height may change based on the pages it navigates to or the content it displays. I have access to the code of the ...

Customizing hover effects for select field options

I need help customizing the background color of a dropdown menu when hovered over. I attempted to assign a class "page-limit-option" to the option element, but it didn't work. Should I create a new style component instead of using the option? <sele ...

Creating Forward Slashes ('/') Is Not Happening When Appending in jQuery

Upon the successful ajax return, I implement this append function during the success state: $('hello').append(' <div class="row" style="background-image: url("/page/12/image-' + user[i]['id'] + '"); height: 155px;" & ...

Best method for developing a jQuery plugin

I've been working on developing my own jquery module, but I want to make sure I'm heading in the right direction before things get too complicated. Although it's not shown in this demo, the main goal is to retrieve content via ajax, so the ...

"Troubleshooting: Click counter in HTML/Javascript unable to function

My latest HTML project is inspired by the "cookie clicker" game, and I'm working on it for a friend. So far, I've managed to get the click counter to function to some extent. Essentially, when you click on the image, the number at the bottom sho ...

Is there a way to have the headline gently fade in and subtly rise when the page loads using CSS?

Is it possible to make the headline fade in and move up slightly on the home page using CSS after the user lands on the website? For a visual reference, check out this example on . I know it can be achieved with translateY, but I have never tried it before ...

Field where tags are generated by user input

I want to incorporate an input field on my website where users can list their skills. These skills should be displayed individually as separate elements on the front end of the site. The desired format for users to input their skills is as follows: skil ...

Leverage the OpenWeather Icons to Pinpoint Locations on a Leaflet Map

Having some trouble incorporating weather icons from an openweather API call as markers on my leaflet map. The icons should appear in a popup on the marker within an img tag. However, despite confirming the correct icon URL through console.log, they only ...

How to retrieve the user ID of the selected user using JqueryUI autocomplete?

Hey there, I'm currently delving into the world of jQuery UI autocomplete. My setup involves using autocomplete with ajax, where PHP helps me fetch a list of users. autocompleteajax.js $(function() { $( "#ranksearch" ).autocomplete({ sou ...

Aligning text in the center of a header, positioned beside an image

I am facing an issue with centering text within a banner. The banner is divided into 12 columns, with a cross icon placed in the left-most column for closing the window. However, the text is not centering within the whole banner width but only within the s ...

Exploring Instagram post layouts: A comparison of card-columns in Chrome versus other web browsers

Utilizing Bootstrap 4 card-columns for showcasing Instagram posts on a website has showcased some discrepancies across different browsers. Notably, Chrome seems to be losing the second column as compared to other browsers. Screenshots highlighting these di ...

issue arising where the table's border is not displaying

I'm confused about why the border of the first tbody tr's td is not visible. https://i.stack.imgur.com/Fwlv7.png I can't see any reason why the border is not showing up. Here's what I've figured out: If the natural height of th ...

What is the best way to ensure consistent image sizes using CSS?

I am struggling to make my 3 images all the same size. Setting the width and height styles is not working for me because the images maintain their aspect ratios, resulting in a messy layout. Here is what I currently have: https://i.sstatic.net/6AWPz.png ...

Passing HTML content to ASP .NET MVC4's Controller

I'm facing an issue where I need to send HTML data to a controller and save it in the database. Despite my attempts, I have been unable to successfully send the HTML data. Code snippet: $("#savechanges").click(function () { $("#notify").val( ...

Is it possible to save jQuery plugin initialization settings for future use?

Is it possible to save a default set of settings for a lightbox jQuery plugin, including options and callback functions, in a variable or array that can be referenced later in different contexts with varying configurations? For example, could I initially ...

I need assistance in locating an error; the error message states that $ is not defined and an object is expected

Issue with $ not being defined, object expected.. I am trying to verify if all sets of radio buttons are checked when a button is clicked! Please help. <script type="text/javascript> $(document).on('click', 'form', function () { ...