• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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.  jQuery first() – Get the First Element
  • 2.  Using jQuery to Change the Text Color of a Paragraph
  • 3.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 4.  Get the Window Scroll Position Using jQuery
  • 5.  Using jQuery to Get the Current URL
  • 6.  Using jQuery to Get the Value of a Radio Button
  • 7.  jQuery appendTo – Insert Element As Last Child
  • 8.  How to Uncheck All Checkboxes in jQuery
  • 9.  Using jQuery to Get the Margin of an Element
  • 10.  jQuery contains – How to Check if a Paragraph Contains Specific Text

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 *

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