• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / jQuery / Using jQuery to Hide a Div

Using jQuery to Hide a Div

November 24, 2021 Leave a Comment

There are a couple of ways we can use jQuery to hide a div. The simplest way is to use the jQuery hide() method.

$("#div1").hide();

We can also use the jQuery css() method to hide a div.

$("#div1").css("display", "none");

Hiding a div using jQuery is very easy using the hide() method.

Let’s say I have the following html:

<div id="div1">This is a div that I can hide with jQuery</div>

Next, we want to hide the div, so we will use the jQuery hide() method. If we wanted to hide the div when the web page initially loads the JavaScript file, it would look like:

$(document).ready(function() {
  $("#div1").hide();
});

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").hide();

Another way you can hide a div using jQuery is to use the jQuery css() method:

$("#div1").css("display", "none");

The jQuery css() method is very useful for changing the styling of a html element dynamically – for example, changing the background color of a div.

Note that we can also hide a div in just plain JavaScript using the display property.

Using jQuery to Hide a Div With a Click

We can use jQuery to hide a div very easily by combining the hide() method with a click event.

Let’s say that we have the following html where we want to give the user the ability to hide “Div 1”:

<div>
  <div id="div1">Div 1</div>
  <div id="div2">Div 2</div>
  <div id="click-me">Hide Div 1</div>
</div>

We can utilize both the jQuery click() method and jQuery hide() method to hide “Div 1”.

Below is the JavaScript code which will allow the user to be able to hide “Div 1”:

$("#click-me").click(function(){
  $("#div1").hide(); // Results in the element #div1 being hidden
}); 

The final code and output for this example of using jQuery to hide a div with a click is below:

Code Output:

Div 1
Div 2

Hide Div 1

Full Code:

<div>
  <div id="div1">Div 1</div>
  <div id="div2">Div 2</div>
  <div id="click-me">Hide Div 1</div>
</div>

<script>

$("#click-me").click(function(){
  $("#div1").hide();
});

</script>

Using the jQuery css() Method to Hide a Div

The jQuery css() method is incredibly useful when using JavaScript to manipulate web pages.

We can use the css() method to easily hide a div.

Let’s say that we have the following html (the same as above) where we want to give the user the ability to hide the shown “Div 1”:

<div>
  <div id="div-1">Div 1</div>
  <div id="div-2">Div 2</div>
  <div id="click-me-1" class="click-me">Hide Div 1</div>
</div>

We will utilize both the jQuery click() method and jQuery css() method to hide “Div 1”.

Below is the JavaScript code which will allow the user to be able to hide “Div 1”:

$("#click-me").click(function(){
  $("#div-1").css("display","none"); // Results in the element #div-1 being hidden
});

The final code and output for this example of how to hide a div using the jQuery css() method is below:

Code Output:

Div 1
Div 2

Hide Div 1

Full Code:

<div>
  <div id="div-1">Div 1</div>
  <div id="div-2">Div 2</div>
  <div id="click-me-1" class="click-me">Hide Div 1</div>
</div>

<script>

$("#click-me-1").click(function(){
  $("#div-1").css("display","none");
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to hide a div.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Move Element After Another
  • 2.  jQuery fadeIn – Fading In Element in HTML
  • 3.  Using jQuery to Select Multiple Ids
  • 4.  Using jQuery to Detect Window Resize
  • 5.  Using jQuery to Replace a Class
  • 6.  Using jQuery to Get Value of Select On Change
  • 7.  Using jQuery prepend to Insert Element As First Child
  • 8.  jQuery appendTo – Insert Element As Last Child
  • 9.  Using jQuery to see if URL Contains Specific Text
  • 10.  jQuery focus – Changing Form Styles with the focus() Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy