remove class after 1 with Jquery

1

I have the following situation where I want to add a class and remove the rest of it from 1

<div class="voted rating-box color ">

</div>

This is the html where the div has 3 class when executing the function I want to remove all the classes but leaving 1 and adding the star class would look like this

<div class="voted star ">

</div>
$(".voted").removeClass().addClass('star');
    
asked by anonymous 29.09.2018 / 00:36

1 answer

4

You were almost able to just add the voted class that you removed:

$(function() {
  $('.voted').removeClass().addClass('voted star');
  console.log($('div').prop('class'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="voted rating-box color">teste</div>
    
29.09.2018 / 00:43