How to hide elements defined as variables in jQuery ? Last Updated : 06 Jun, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to hide elements defined as variables in jQuery. These can be done using two approaches. Approach 1: In this approach, we will first select the element that has to be hidden and then assign it to a variable. We will then call the hide() method on the variable. This method will hide the element from the page. Example: HTML <!DOCTYPE html> <html> <head> <script src= "https://code.jquery.com/jquery-3.6.0.js"> </script> <script> $(document).ready(function () { $("button").click(function () { // Getting the element with the id // of "dsa" in a variable let dsaGFG = $("#dsa"); // Hiding the element using the // hide() method dsaGFG.hide(); }) }); </script> </head> <body> <h1 style="color: green;"> GeeksforGeeks </h1> <p id="faang">FAANG</p> <p id="dsa">DSA</p> <p id="cp">CP</p> <p id="algo">ALGO</p> <button>Hide Element</button> </body> </html> Output: Approach 2: In this approach, we will first select the element that has to be hidden and then assign it to a variable. We will then call the addClass() method on the variable. This will add a CSS class that we will create next. This CSS class will contain the display property that is set to none, effectively hiding the element. Example: HTML <!DOCTYPE html> <html> <head> <style> /* Define the class to be added */ .hiddenClass { /* Setting the display to none hides the element */ display: none; } </style> <script src= "https://code.jquery.com/jquery-3.6.0.js"> </script> <script> $(document).ready(function () { $("button").click(function () { // Getting the element with the id // of "cp" in a variable let cpGFG = $("#cp"); // Hiding the element by adding a // class using the addClass() method cpGFG.addClass("hiddenClass"); }) }); </script> </head> <body> <h1 style="color: green;"> GeeksforGeeks </h1> <p id="faang">FAANG</p> <p id="dsa">DSA</p> <p id="cp">CP</p> <p id="algo">ALGO</p> <button>Hide Element</button> </body> </html> Output: Comment More infoAdvertise with us Next Article How to hide elements defined as variables in jQuery ? R rajatagrawal5 Follow Improve Article Tags : Web Technologies JQuery HTML-Tags CSS-Properties jQuery-Methods jQuery-Questions +2 More Similar Reads How to check if an element is hidden in jQuery? To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element. Syntax:$(element).is(":hidden");Example:html<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Jquery Hid 1 min read How to show/hide an element using jQuery ? We are given an element and the task is to to show/hide the element using jQuery. Below are the approaches to show/hide an element using jQuery:Table of ContentUsing css() methodsUsing show methodUsing Toggle methodApproach 1: Using css() methodsIt takes two parameters where the first parameter is t 3 min read How to Hide an HTML Element in Mobile View using jQuery ? Suppose we have given an HTML document and the task is to hide an HTML element in mobile view with the help of jQuery. Approach: Initially, we are required to detect whether our system is 'Mobile' or not, for that window.navigator.userAgent property is used. It returns a string containing informatio 2 min read How to find all elements that are disabled in jQuery ? In this article, we will learn how to find all elements on the page that are disabled using jQuery. Approach: The :disabled selector can be used to get all elements on the page that are currently disabled. These elements are iterated over using the each() method. The elements can then be individuall 2 min read How to hide div element after few seconds in jQuery ? Given a div element, the task is to hide the div element after a few seconds using jQuery. Approach:Select the div element.Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.Example 1: In this example, the setTimeOut() method is used to provide a delay to the f 2 min read Like