WinJS.Utilities.toggleClass function

Toggles (adds or removes) the specified class on the specified element. If the class is present, it is removed; if it is absent, it is added.

Syntax

var htmlElement = WinJS.Utilities.toggleClass(e, name);

Parameters

  • e
    Type: HTMLElement

    The element on which to toggle the class.

  • name
    Type: string

    The name of the class to toggle.

Return value

Type: HTMLElement

The element.

Examples

<div id="div1"></div>
<script type="text/javascript">
    var firstElement = document.getElementById("div1");
    WinJS.Utilities.addClass(firstElement, "testClass");
    var element = WinJS.Utilities.toggleClass(firstElement, "testClass");
    // This returns false.
    var hasClass = WinJS.Utilities.hasClass(firstElement, "testClass");

    WinJS.Utilities.toggleClass(firstElement, "testClass");
    // This returns true.
    hasClass = WinJS.Utilities.hasClass(firstElement, "testClass");
</script>

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Utilities