0

i am developing a module in PHP and i need to add/remove class at the same time using event onclick in javascript. Please suggest.

Below is the code but it is not working,

jQuery("#cat-block-id").show();
 //jQuery("#product-block-id").hide();
jQuery(".fme_layered_attribute").click(function() {
    jQuery("#product-block-id").show();
//jQuery("#cat-block-id").show();
})

Thanks,

1
  • are you using a javascript library such as jquery or prototype? Commented Jan 27, 2012 at 10:58

3 Answers 3

3

Here is a solution with jquery :

$('.currentClass').removeClass('currentClass').addClass('newClass');
Sign up to request clarification or add additional context in comments.

Comments

0

Are you looking for something like

$(myElement).click(function () {
    this.addClass("className1");
    // ...
    this.removeClass("className2");
});

Comments

0

Are you using jQuery?

If not, then I suggest to use jQuery, which is very easy to use. If yes, then you can use this:

$(element).removeClass('currentClass').addClass('newClass');

Since jQuery supports that, you can chain functions.

EDIT:
Alternatively, you can get the CSS classes and split them by a space (one or possibly multiple) resulting into an array. Then, you can just remove the desired value out of the array, insert the desired class to the array, and join that array with a space as glue. Then set this string as the class attribute of an HTML element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.