Reputation: 684
I have button with ID "searchBtn". Each time the button is clicked, the class should change. I want to rotate through a predefined number of classes on each click.
How would I go about doing this?
Upvotes: 2
Views: 197
Reputation: 32949
you may try the jquery's toggleClass function as follows:
$(document).ready(function(){
$('#searchBtn').click(function () {
$(this).toggleClass('class_x','class_y');
});
});
Upvotes: 2
Reputation: 3875
var counter = 0;
$("#searchBtn").click(function(){
$(SELECTOR).removeClass("class"+counter++);
$(SELECTOR).addClass("class"+counter);
});
Upvotes: 0
Reputation: 5050
Give something like this a try:
$('#searchBtn').on('click', function () {
if ($(this).hasClass('classA')) {
$(this).removeClass('classA').addClass('classB');
} else {
$(this).removeClass('classB').addClass('classA');
}
});
Upvotes: 1
Reputation: 10219
$('#searchBtn').on('click', function() {
$(this).removeClass('oldClass');
$(this).addClass('newClass');
});
Upvotes: 0