remove class after 1 with Jquery

Asked

Viewed 43 times

1

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

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

</div>

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

<div class="voted star ">

</div>

$(".voted").removeClass().addClass('star');

1 answer

4


You were almost getting just missed adding again the class voted who had 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>

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

  • 1

    thanks worked out it was so easy .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.