9951 explained code solutions for 126 technologies


jqueryHow do I use jQuery to toggle an element?


jQuery provides a method called toggle() that allows you to toggle an element. This method takes an optional parameter which is a boolean value that determines whether to show or hide the element.

Example code

$("#myElement").toggle();

The code above will hide the element with the ID of myElement. To show it again, you can call the same code again:

$("#myElement").toggle();

You can also pass in a boolean parameter to specify whether to show or hide the element. true will show the element and false will hide it:

$("#myElement").toggle(true); // Show the element
$("#myElement").toggle(false); // Hide the element

This is useful when you want to toggle the element based on a certain condition. For example, if you wanted to show the element if a certain variable is true:

if (someVariable) {
  $("#myElement").toggle(true);
}

You can also use the toggle() method to toggle multiple elements at once. To do this, pass in a second parameter which is a function that will be called for each element in the selection:

$("div").toggle(function() {
  $(this).hide();
});

The code above will hide all div elements.

Helpful links

Edit this code on GitHub