How to Use JQuery toggle() With Example?

You can also hide and show the element through the toggle method.In jQuery it is defined by toggle ().

JQuery toggle() Method, Use JQuery toggle() Function

It simply means to show the element which is hide and hide the element which is show.But now you must be thinking that we were doing this earlier also, what is new in this?So new in this is that all this will be done through a single button.

For example, let us see a code:

Syntax:-

$(selector).toggle(speed,callback);

Example:-

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<button>Toggle between hiding and showing</button>
<p>This is a paragraph with experts php.</p>
<p>This is another experts php.</p>
</body>
</html>