Jquery Remove Class Multiple Elements

wp supprt

I am setting a class to multiple elements via a Button click via the code below:

$('button.but1').on('click', function() {
    $(".div1,.div2,.div3").addClass("focus");
    $(".div1,.div2,.div3").css("z-index", "99");
    $(".div1,.div2,.div3").css("opacity", "1");
});
$('button.but2').on('click', function() {
    $(".div4,.div5,.div6").addClass("focus");
    $(".div4,.div5,.div6").css("z-index", "99");
    $(".div4,.div5,.div6").css("opacity", "1");
});

HTML:

<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>
<div class="div5"></div>
<div class="div6"></div>
<button type="button" class="but1" href="#">But1</button>
<button type="button" class="but2" href="#">But2</button>

The issue I am having is that I only want the div selected to be "focus"(the class), all the other div, not from the same group need to have the "focus" class removed. Not sure which we to go?

Milind Anantwar

You can remove the already added class before adding the class to set of elements.like this:

 $(".focus").removeClass("focus");
 $(".div4,.div5,.div6").addClass("focus");

Complete Code:

$('button.but1').on('click', function() {
  $(".focus").removeClass("focus");
  $(".div1,.div2,.div3").addClass("focus");
  $(".div1,.div2,.div3").css("z-index", "99");
  $(".div1,.div2,.div3").css("opacity", "1");
});

$('button.but2').on('click', function() {
  $(".focus").removeClass("focus");
  $(".div4,.div5,.div6").addClass("focus");
  $(".div4,.div5,.div6").css("z-index", "99");
  $(".div4,.div5,.div6").css("opacity", "1");
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add and Remove Class from multiple elements with jQuery

Remove class from multiple elements in JavaScript or jQuery

Remove multiple elements in jquery

Remove duplicate elements by class with JQuery

Combine multiple elements by class in jQuery

multiple elements toggle second click remove class

How to remove empty elements after class?(jQuery)

JQuery to remove class from sibling's elements

jquery unable to remove table elements by class name

Remove multiple elements from array in Javascript/jQuery

Add and remove classes for multiple elements (JQuery & JavaScript)

jQuery .text() multiple elements same class

jQuery Check if multiple elements have a class

Jquery matching multiple elements with same id/class

jQuery - Click Add/Remove Class - Multiple Buttons

Add and remove class from multiple jquery selectors

How to remove class from multiple elements in reverse order?

How to remove CSS class from multiple ID elements using JS?

how to add or remove class from html elements with jQuery

How to add and remove active class to elements that are not siblings? (jQuery traversing)

Remove specific class from group of elements without jquery

How to remove elements of a certain class inside a parent element Jquery

jQuery - Combining multiple .find() methods and takes elements out of the DOM with .remove()

How to check multiple elements has same class using jquery?

affect only the clicked element in jquery for multiple elements with same class

Set html contents of an element using jQuery but with multiple elements of the same class

How can I add a conditional class to multiple elements using jQuery?

Same class name for multiple elements but execute event independently in pure jQuery

jQuery/Javascript get classes of multiple elements to show an element with this class