God
God

Reputation: 684

How to write two class for single id in jQuery?

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

Answers (4)

Amyth
Amyth

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

Infinity
Infinity

Reputation: 3875

var counter = 0;

$("#searchBtn").click(function(){

 $(SELECTOR).removeClass("class"+counter++);
 $(SELECTOR).addClass("class"+counter);

});

Upvotes: 0

PhearOfRayne
PhearOfRayne

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

David Jones
David Jones

Reputation: 10219

$('#searchBtn').on('click', function() {
  $(this).removeClass('oldClass');
  $(this).addClass('newClass');
});

Upvotes: 0

Related Questions