Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery add CSS class

Tags:

jquery

css

I want to remove a css class before adding a new class to an element. If I know which exact class the element has already, I can remove that by

$('#test').removeClass('current_class').addClass('new_class');

But the problem is that #test does not have always same class. So I would like to remove any css class attached attached to #test before adding before adding a new one. How should I do that? Thanks.

like image 367
user1117313 Avatar asked Dec 16 '22 01:12

user1117313


2 Answers

$('#test').removeClass();

Will remove any class that the item has you can simply add your new class after that like you are doing now

like image 150
Teun Pronk Avatar answered Jan 15 '23 05:01

Teun Pronk


$('#test').attr('class', '').addClass('new_class');

or

$('#test').removeClass().addClass('new_class');
like image 41
Riz Avatar answered Jan 15 '23 04:01

Riz