• 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 / jQuery Display None

jQuery Display None

July 28, 2022 Leave a Comment

We can use jQuery display none to easily hide an HTML element. We can do this by using the jQuery css() method to target the display property of an element and change it to none.

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

We can also get the same result by using the jQuery hide() method, which requires a little less code.

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

Hiding a Div using jQuery is very easy using the css() method and targeting the display property.

Let’s say we have the following HTML:

<div id="div1">This is a Div that we can hide with jQuery</div>
<div id="div2">This is a Div that we will NOT can hide</div>

We want to hide the div with id #div1, so we will use the jQuery css() method and set the display property to none. If we wanted to hide the div when the web page initially loads the JavaScript file, it would look like this:

$(document).ready(function() {
  $("#div1").css("display","none");
});

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

jQuery("#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.

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

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

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

Using jQuery Display None With a Click

We can use jQuery to hide a div very easily by combining the css() 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 “#div1”:

<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 css() method to set the display property of “Div 1” to none, which will hide the div.

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

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

The final code and output for this example of using jQuery display none 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").css("display","none");
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery display none to hide an element.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Scroll to Div
  • 2.  jQuery parent – Get the Parent Element of a Div
  • 3.  Using jQuery to Toggle Class of HTML Element
  • 4.  Using jQuery to Move Element After Another
  • 5.  Using jQuery to Scroll to Top
  • 6.  How to Set Checkbox Checked With jQuery
  • 7.  How to Check if Element Exists Using jQuery
  • 8.  Using jQuery to Add Option to Select List
  • 9.  jQuery mouseout – An Interactive Example of the mouseout Method
  • 10.  jQuery siblings – Get the Sibling Elements of a Div

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