How to remove a class in a list of elements and add to another element Jquery?

Hailei

html. The "active" class may be with the "render1", "render2", "render3" or "render4". It is with "render2" in this case:

<td class="render1">
<td class="render2 active">
<td class="render3">
<td class="render1">
<td class="tag1 active">
<td class="tag2 active">

I did this way...

 $(".render1").on("change", function(){
   $(".render1").removeClass().addClass("active");
   $(".render2").removeClass();
   $(".render3").removeClass();
   $(".render4").removeClass();
 });
$(".render2").on("change", function(){
   $(".render1").removeClass();
   $(".render2").removeClass().addClass("active");
   $(".render3").removeClass();
   $(".render4").removeClass();
 });
 $(".render3").on("change", function(){
   $(".render1").removeClass();
   $(".render2").removeClass();
   $(".render3").removeClass().addClass("active");
   $(".render4").removeClass();
 });
 $(".render4").on("change", function(){
   $(".render1").removeClass();
   $(".render2").removeClass();
   $(".render3").removeClass();
   $(".render4").removeClass().addClass("active");
 });

If I click "render3", the class it is removed from "render2" class and added to "render3". So the html will be end like this:

<td class="render1">
<td class="render2">
<td class="render3 active">
<td class="render1">
<td class="tag1 active">
<td class="tag2 active">

It is not really working on my computer. Is there a better way to do this? Thanks a lot!

Sam Thornton

+1 to both @Wilmer and @marsh. I'll also add that you can DRY up your code by just calling on 'click' for any valid element (you can add a common class between them to make it easy, but in the example I'll just use td):

$('td').on('click', function() {
  $('.active').removeClass('active');
  $(this).addClass('active');
});

Though, if you want to only deal with the same 'type' of (e.g. .renderX or .tagX), I'd add a class to each.

<td class="render render1"></td>
<td class="render render2 active"></td>
<td class="tag tag1 active"></td>
<td class="tag tag2></td>

And then:

$('.render').on('click', function() {
  $('.render.active').removeClass('active');
  $(this).addClass('active');
});

Or, alternatively, check for class similarities (and no need to add extra classes):

$('td').on('click', function() {
  var classNameType = $(this).attr('class').replace(/\d|\s|active/gi, '');
  var selector = "[class*=" + classNameType + "]";
  $(selector).removeClass('active');
  $(this).addClass('active');
});

Here's an example of that last one: http://codepen.io/anon/pen/gbBNpV

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jquery remove and add class in list element

From Dev

How to remove class and add another class jquery

From Dev

How to add and remove a class if another element is clicked in AngularJS?

From Dev

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

From Dev

how to add or remove class from html elements with jQuery

From Dev

how to add or remove class from html elements with jQuery

From Dev

add/remove class in jquery and scroll to element

From Dev

jQuery: If class exists, remove it, then add it to new element

From Dev

Add an element from another class to an array list

From Dev

how to add a class to one a element and remove a class from all siblings a elements angular

From Dev

How to add a class to element if another element is empty?

From Dev

jQuery: Dynamically add class that belongs to another element

From Dev

AngularJS Directive to add class on click, but remove and add it to another element if clicked

From Dev

AngularJS Directive to add class on click, but remove and add it to another element if clicked

From Dev

How to add element to a list, inside another list?

From Dev

add/remove active class for ul list with jquery?

From Dev

jQuery add remove class on list with a delay in between

From Dev

How to target elements in another element using jQuery

From Dev

How to add class to a jQuery element if a condition is true and remove the same class if the condition is false?

From Dev

How to add | remove css class in angular element?

From Dev

jQuery filter elements and add class if class not present and if it's the first element

From Dev

How to remove empty elements after class?(jQuery)

From Dev

How to add a class to an element when another element gets a class in angular?

From Dev

Angular 2+ Add or Remove a Class of an element that belongs to another component

From Dev

How to dynamic add and remove element by jQuery?

From Dev

Remove elements of a list if is in another list

From Dev

How to remove class before leaving a element in jQuery

From Dev

Thread safe remove/add element from one list to another

From Dev

jQuery add class and remove it for all the elements who doesnt have it

Related Related

  1. 1

    jquery remove and add class in list element

  2. 2

    How to remove class and add another class jquery

  3. 3

    How to add and remove a class if another element is clicked in AngularJS?

  4. 4

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

  5. 5

    how to add or remove class from html elements with jQuery

  6. 6

    how to add or remove class from html elements with jQuery

  7. 7

    add/remove class in jquery and scroll to element

  8. 8

    jQuery: If class exists, remove it, then add it to new element

  9. 9

    Add an element from another class to an array list

  10. 10

    how to add a class to one a element and remove a class from all siblings a elements angular

  11. 11

    How to add a class to element if another element is empty?

  12. 12

    jQuery: Dynamically add class that belongs to another element

  13. 13

    AngularJS Directive to add class on click, but remove and add it to another element if clicked

  14. 14

    AngularJS Directive to add class on click, but remove and add it to another element if clicked

  15. 15

    How to add element to a list, inside another list?

  16. 16

    add/remove active class for ul list with jquery?

  17. 17

    jQuery add remove class on list with a delay in between

  18. 18

    How to target elements in another element using jQuery

  19. 19

    How to add class to a jQuery element if a condition is true and remove the same class if the condition is false?

  20. 20

    How to add | remove css class in angular element?

  21. 21

    jQuery filter elements and add class if class not present and if it's the first element

  22. 22

    How to remove empty elements after class?(jQuery)

  23. 23

    How to add a class to an element when another element gets a class in angular?

  24. 24

    Angular 2+ Add or Remove a Class of an element that belongs to another component

  25. 25

    How to dynamic add and remove element by jQuery?

  26. 26

    Remove elements of a list if is in another list

  27. 27

    How to remove class before leaving a element in jQuery

  28. 28

    Thread safe remove/add element from one list to another

  29. 29

    jQuery add class and remove it for all the elements who doesnt have it

HotTag

Archive