Question

All my <p>s have a class "A". I want to change the class of a <p> when it's clicked to class "B" and reset all others to class "A". Easy to change the <p>s to class "B", but completely stuck with reverting the others.

Was it helpful?

Solution

$('p').click(function() {
    $('p').removeClass('B').addClass('A'); // revert all 'p's to 'A'
    $(this).removeClass('A').addClass('B'); // 'this' refers to the clicked object
});

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top