• 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
  • Ruby
  • 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.  How to Clear an Input Field Using jQuery
  • 2.  Using jQuery to Add Required Attribute to Input Field
  • 3.  jQuery Display None
  • 4.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 5.  How to Use jQuery to Clear a textarea Field
  • 6.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 7.  Get the Height of a Div Using jQuery
  • 8.  jQuery Get Last Child
  • 9.  Using jQuery to Count Children of an Element
  • 10.  Using jQuery to Hide Element by Class

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy