Add multiple classes to an element using .attr?

Ber B

I am currently trying to add multiple classes from a variable created when the user selects checkboxes to an element using $('.element').attr but this doesn't seem to be working so far - this method seems to be only getting the first class in the array, .addClass works only to add the classes and not remove them.

Is there any workaround to this?

The Code used is:

$('.ms-drop').on('change', function(){

        var val = [];

        var selectedItem  = $(this).find('input').attr('value');

        //get the values of each checked checkbox
        $(this).find('li > label > input:checked').each(function(i){
            val[i] = $(this).val();
        });

        var new_class = val.join(' ');

        console.log(new_class);

        $('.editable').attr('class', new_class);

        $('.writeHere').html(new_class);

});

Please see the link below to see it working:

JSBin

Any help will be greatly appreciated!

Rhumborl

I would listen to the input onchange instead and just use toggleClass:

$('.ms-drop input[type="checkbox"]').on('change', function(){
  $('.editable').toggleClass($(this).val());
  $('.editable').html($('.editable').attr('class')); // for debug purposes
});
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
  <section class="ms-drop">
    <ul>
      <li>
        <label>
          <input type="checkbox" value="Class1">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" value="Class2">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" value="Class3">
        </label>
      </li>
      <li>
        <label>
          <input type="checkbox" value="Class4">
        </label>
      </li>
    </ul>
  </section>
  
  
  <div class="editable">
    Class: <span class="writeHere">Hehe</span>
  </div>
  
  
</body>
</html>

EDIT If other code can change the style of .editable as @Roamer-1888 mentions, you can test the checked state of the box and decide to add or remove, rather than just toggle (although anything else changing the classes would make the checkboxes out of sync anyway which is a functional bug in itself):

$('.ms-drop input[type="checkbox"]').on('change', function(){
  if($(this).is(':checked'))
    $('.editable').addClass($(this).val());
  else
    $('.editable').removeClass($(this).val());

  $('.editable').html($('.editable').attr('class')); // for debug purposes
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Conditionally add multiple classes to element

From Dev

Conditionally add multiple classes to element

From Dev

Using html:not when element has multiple classes

From Dev

Dynamically add specific attr to element

From Java

How to add multiple classes in Material UI using the classes props

From Dev

Can I add multiple classes to a div element? (CSS)

From Dev

Add and remove multiple classes to an element on scroll, reversing animation

From Dev

How to add multiple classes, one with incrementing numbers, on a dynamic element?

From Dev

attr_accessor across multiple classes?

From Dev

Add multiple html element dynamically using jquery

From Dev

Using custom attr to add css?

From Dev

jquery attr add id to existing element

From Dev

add all data attr from element in array

From Dev

select specific element using jQuery attr() Method

From Dev

JS - Add an array of classes to an element

From Dev

Add classes to html element with jQuery

From Dev

IMacos - Extracting element with multiple classes

From Dev

Selecting an element with multiple classes in jQuery

From Dev

How to add multiple attribute in single element using xsd

From Dev

Using multiple classes in java?

From Dev

Multiple inheritance using classes

From Dev

How to find a spesific class,and find a spesific element and add disabled attr to it

From Dev

Add Classes to an SVG element after using PHP's file_get_contents

From Dev

Using beautifulsoup how do I remove a single class from an element with multiple classes

From Dev

Using beautifulsoup how do I remove a single class from an element with multiple classes

From Dev

How can I add two classes to an element

From Dev

Add css classes in sequence to div element

From Dev

Add classes to Wordpress Menu ul element

From Dev

Using attr(data-icon) property to display unicode before element

Related Related

  1. 1

    Conditionally add multiple classes to element

  2. 2

    Conditionally add multiple classes to element

  3. 3

    Using html:not when element has multiple classes

  4. 4

    Dynamically add specific attr to element

  5. 5

    How to add multiple classes in Material UI using the classes props

  6. 6

    Can I add multiple classes to a div element? (CSS)

  7. 7

    Add and remove multiple classes to an element on scroll, reversing animation

  8. 8

    How to add multiple classes, one with incrementing numbers, on a dynamic element?

  9. 9

    attr_accessor across multiple classes?

  10. 10

    Add multiple html element dynamically using jquery

  11. 11

    Using custom attr to add css?

  12. 12

    jquery attr add id to existing element

  13. 13

    add all data attr from element in array

  14. 14

    select specific element using jQuery attr() Method

  15. 15

    JS - Add an array of classes to an element

  16. 16

    Add classes to html element with jQuery

  17. 17

    IMacos - Extracting element with multiple classes

  18. 18

    Selecting an element with multiple classes in jQuery

  19. 19

    How to add multiple attribute in single element using xsd

  20. 20

    Using multiple classes in java?

  21. 21

    Multiple inheritance using classes

  22. 22

    How to find a spesific class,and find a spesific element and add disabled attr to it

  23. 23

    Add Classes to an SVG element after using PHP's file_get_contents

  24. 24

    Using beautifulsoup how do I remove a single class from an element with multiple classes

  25. 25

    Using beautifulsoup how do I remove a single class from an element with multiple classes

  26. 26

    How can I add two classes to an element

  27. 27

    Add css classes in sequence to div element

  28. 28

    Add classes to Wordpress Menu ul element

  29. 29

    Using attr(data-icon) property to display unicode before element

HotTag

Archive