Question

I have Simple Table with some TDs having Class as 'PLUS' and Some TDs with class 'MINUS'.

I am implementing the Collapse all and Expand All functionality using jquery.

On click of Expand All, I want to replace 'PLUS' class with 'MINUS' of only TDs who has 'PLUS' class.

and on click of Collapse All, replace 'MINUS' class with 'PLUS'

I am facing problem for finding all TDs with plus class and iterate through all of them to replace class

Any Suggestions please?

Was it helpful?

Solution

There is no need to iterate, jQuery will do this for you very conveniently.

It's not very clear from the question what exactly you want to do, but you can:

  • Switch .plus to .minus with

    $(".plus").toggleClass("plus minus")
    
  • Toggle .plus and .minus on all elements that have either with

    $(".plus, .minus").toggleClass("plus minus")
    

OTHER TIPS

A selector for an element with a class is {tag}.{class} http://css.maxdesign.com.au/selectutorial/selectors_class.htm

// When clicked, replace all td.plus with td.minus
$('td.plus').click(function(){
    $('td.plus').toggleClass('plus minus')
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top