toggle() method in jquery

toggle() in jQuery

toggle() method is used to toggle between show() and hide() method.

When using this method, specified elements can be made hidden, similarly the hidden element will be made visible.

Parameters may or may not be used in toggle() method.

Syntax 1


When using the above syntax, elements will be toggled between both showing and hiding immediately without any animation.

Syntax 2


  • duration
    • Default value is 400.
    • number - Integer values are given in milliseconds.
    • slow - It specifies 600 milliseconds.
    • fast - It specifies 200 milliseconds.
  • easing - It is optional, it specifies speed at which the animation progresses.It can have two values.
    • swing - It is a default value, animation progresses irregular way.
    • linear - it specifies animation progresses at a constant pace.
  • complete - It indicate a function, and it will be executed after the element animation complete.
Syntax 3


  • options - Options given below are optional
    • duration - It is used to determine how long the animation will run. It can be defined by string or number.
    • easing - It is used to determine for transition which easing function to be used. It is defined by string.
    • queue - It is denoted by Boolean, whether to place the animation in the effects queue. When a custom queue name is used the animation does not automatically start; you must call .dequeue("queuename") to start it. If it is specified as false, the animation will begin immediately.
    • specialEasing - It is an object which contains one or more of the CSS properties. These properties are defined by arguments and its corresponding easing functions.
    • step - It is used to denote a function for each element when calling animated property.
    • progress - It is used to denote a function to be called after each step of the animation.
    • complete - It is used to denote a function, which is called after the animation on an element is complete.
    • start - It is used to denote a function, when the elements animation begins.
    • done - It is used to denote a function, when the animation on an element completes.
    • fail - It is used to denote a function, when the animation of an element fails to complete.
    • always - It is used to denote a function, when the animation on an element completes or stops without completing.
Syntax 4


  • display
    • true - to show the element.
    • false - to hide the element.

Example 1- show or hide the element

Try Yourself

Example 2 - show or hide element within specified milliseconds

Try Yourself

Example 3 - toggle() with complete function

Try Yourself

Example 4 - calling complete function using function name

Try Yourself

Example 5 - show or hide element using easing(linear) value

Try Yourself

Example 6 - setting toggle true or false

Try Yourself

Example 7 - toggle() with options

Try Yourself

Example 8 - toggle() with options and progress bar

Try Yourself