• 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 Element by Class

Using jQuery to Hide Element by Class

April 10, 2022 Leave a Comment

The easiest way we can use jQuery to hide an element by its class name is to use the jQuery hide() method.

$(".class-name").hide();

We can also use jQuery to hide an element by its class name by using the jQuery css() method.

$(".class-name").css("display", "none");

Let’s say we have the following html:

<div id="div1">
  <p class="do-not-hide">This is a class that we do NOT want to hide with jQuery</p>
  <p class="hide">This is a class that we WANT to hide with jQuery</p>
  <p class="hide">This is a class that we WANT to hide with jQuery</p>
  <p class="do-not-hide">This is a class that we do NOT want to hide with jQuery</p>
</div>

Next, if we want to hide all the divs with classname hide, we can use the jQuery hide() method. Here is the JavaScript code to make that happen.

$(".hide").hide();

We can also do this using the css() method in the following jQuery code:

$(".hide").css("display", "none");

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

jQuery(".hide").css("display", "none");

Note that we can also hide classes in just plain JavaScript.

Using jQuery to Hide Elements by their Class Name with a Click

We can use jQuery to hide elements by their class name 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 the divs with classname “class-to-hide”:

<div id="div1">
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <div id="click-me">Hide classes</div>
</div>

We can utilize both the jQuery click() method and jQuery hide() method to hide elements with class name class-to-hide.

Below is the JavaScript code which will allow the user to be able to hide these elements:

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

The final code and output for this example of using jQuery to hide elements by their class name is below:

Code Output:

This is a class that we do NOT want to hide with jQuery

This is a class that we WANT to hide with jQuery

This is a class that we WANT to hide with jQuery

This is a class that we do NOT want to hide with jQuery

This is a class that we WANT to hide with jQuery

This is a class that we do NOT want to hide with jQuery

This is a class that we WANT to hide with jQuery

This is a class that we do NOT want to hide with jQuery

This is a class that we do NOT want to hide with jQuery

Hide classes

Full Code:

<div id="div1">
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p class="class-to-hide">This is a class that we WANT to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <p>This is a class that we do NOT want to hide with jQuery</p>
  <div id="click-me">Hide classes</div>
</div>

<script>

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

</script>

Hopefully this article has been useful for you to understand how to use jQuery to hide an element by its class name.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Convert a String to Uppercase
  • 2.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 3.  jQuery fadeIn – Fading In Element in HTML
  • 4.  How to Set Checkbox Checked With jQuery
  • 5.  Using jQuery to Get the Top Position of Element
  • 6.  Using jQuery to Change the Font Size of a Paragraph
  • 7.  Using jQuery to Set the Id of a Div
  • 8.  Examples of Text Animations Using jQuery
  • 9.  Using jQuery to Capitalize the First Letter of a String
  • 10.  Using jQuery to Append Text to textarea Field

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