Skip to content
the poor coder

jquery toggle visibility

Poor Man
Published
Reading time
2 min read
On this page

jQuery Toggle Visibility#

If you want to toggle the visibility of an HTML element using jQuery, there are a few ways to accomplish this. The easiest way is to use the .toggle() function.

Using .toggle()#

The .toggle() function toggles the display property of elements. If an element is hidden with display: none, .toggle() will display the element. If an element is visible with display: block, .toggle() will hide the element.

Text

$('button').click(function(){
  $('p').toggle();
});
  • $('button'): selects the button element
  • click(function(){}): attaches an event listener for when the button is clicked
  • $('p'): selects the paragraph element(s) to toggle
  • .toggle(): toggles the visibility of the paragraph element(s)

Using .show() and .hide()#

You can also use the .show() and .hide() functions to show or hide elements respectively.

Text

$('button').click(function(){
  $('p').toggle();
});
  • $('button'): selects the button element
  • click(function(){}): attaches an event listener for when the button is clicked
  • $('p'): selects the paragraph element(s) to toggle
  • .toggle(): toggles the visibility of the paragraph element(s)

Using .show() and .hide() gives you more control over the display property of elements. You can specify the display property to use when showing or hiding the element.

Text

$('button').click(function(){
  $('p').toggle("slow");
});
  • $('button'): selects the button element
  • click(function(){}): attaches an event listener for when the button is clicked
  • $('p'): selects the paragraph element(s) to toggle
  • .toggle("slow"): toggles the visibility of the paragraph element(s) with a slow animation

In this example, the .toggle() function will show the element with a slow animation when it is hidden, and hide the element with a slow animation when it is visible.

Using .show() and .hide() also allows you to specify a callback function to execute when the animation is complete.

Text

$('button').click(function(){
  $('p').toggle("slow", function(){
    alert("Animation complete.");
  });
});
  • $('button'): selects the button element
  • click(function(){}): attaches an event listener for when the button is clicked
  • $('p'): selects the paragraph element(s) to toggle
  • .toggle("slow", function(){}): toggles the visibility of the paragraph element(s) with a slow animation, and executes the callback function when the animation is complete
  • alert("Animation complete.");: the callback function to execute when the animation is complete

In this example, the .toggle() function will show the element with a slow animation when it is hidden, and hide the element with a slow animation when it is visible. When the animation is complete, an alert box will appear.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC