Search code examples
jqueryarraysremoveclass

jQuery - remove a class that is in an array


I have a div object

<div class="class1 classA classB class2"> content </div>

and some buttons

<button id="numbers">clickme N </button>

<button id="alphas">clickme A </button>

The code should do this function:

<script>
    var classesN = [class1, class2, class3];
    var classesAlpha = [classA, classB, classC];

    $('#alphas').click(function() {
       $('div').removeClass(all classes that are in the classesAlpha array);
    });
    $('#numbers').click(function() {
       $('div').removeClass(all classes that are in the classesN array);
    })
</script>

Thanks for any suggestion and I hope you like the question :)


Solution

  • var classesN = ["class1", "class2", "class3"];
    var classesAlpha = ["classA", "classB", "classC"];
    
    $('#alphas').click(function() {
        var $div = $('div');
        $.each(classesAlpha, function(i, v){
           $div.removeClass(v);
        });
    });
    

    DEMO