Home >>jQuery Tutorial >jQuery toggle() Method

jQuery toggle() Method

jQuery toggle() Method

jQuery toggle() method in jQuery is used to toggles between hide() and show() for the selected elements.

This method searches for visibility on selected components. Show() executes when the element is hidden. Hide() is executed when an element is available-this generates a toggle.

Syntax:
$(selector).toggle(speed,easing,callback)

Parameter Values

Parameter Description
speed It is an optional parameter and is used to Specify the speed of the hide/show effect.

Possible values:

  • milliseconds
  • "slow"
  • "fast"
easing It is an optional parameter and is Specify the speed of the element in points of the animation. Its Default value is "swing"

Possible values:

  • "swing" - faster in the middle, moves slower at the beginning
  • "linear" – it moves in a constant speed
callback It is an optional parameter. After the toggle() method is completed a function to be executed
Here is an Example of jQuery toggle() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn").click(function(){
$(".toggle").toggle();
});
});
</script>
</head>
<body>
<h3 class="toggle">Phptpoint</h3>
<button class="btn">Toggle</button>
</body>
</html>

Output:

Phptpoint


No Sidebar ads