Skip to content

Element toggleclass

maxkfranz edited this page Nov 28, 2011 · 2 revisions

ele.toggleClass()

Toggle whether an element has a class.

ele.toggleClass( classNames )

  • classNames : A space-separated list of class names to toggle

ele.toggleClass( classNames, toggle )

  • classNames : A space-separated list of class names to toggle
  • toggle : If true, adds the class; if false, removes the class

Details

Without the toggle argument specified, the class is automatically toggled based on whether the element already has the classes specified. For each class specified, if the element has the class, the class is removed. Otherwise the class is added.

Examples

var n1 = cy.node("n1");

n1.addClass("foo");
n1.removeClass("bar");

n1.toggleClass("foo"); // n1 has neither foo nor bar
n1.toggleClass("bar"); // bar added
n1.toggleClass("bar", true); // add bar, but bar is already there; no effect
n1.toggleClass("bar", false); // removed bar

Clone this wiki locally