How to make jQuery throw an error when it doesn't match an element ? Last Updated : 26 May, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to make a jQuery throw error when it does not match an element. Approach: To do this task we create a function and check if the given element exists or not, using the length property of the element. If an element exists the length of the element is greater than or equal to 1 otherwise the length of the element is 0. So if the element exists we display a message otherwise we throw an error. We call the function before we use that element. If the element doesn't exist then we get an error. Example: HTML <!DOCTYPE html> <html> <head> <!-- JQuery CDN --> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> </head> <body> <h2 style="color:green">GeeksforGeeks</h2> <b>Check for element existence using jQuery</b><br/> <ul> <li class="gfg1">CSS</li> <li class="gfg3">HTML</li> <li class="gfg4">JQuery</li> </ul> <div id="resultID"></div> <script> // Create a function that checks if the element exists or not. $.fn.check = function(){ if(this.length === 0){ $("#resultID").show().html("This element does not exist!"); } else { $("#resultID").show().html("This element exist!"); } } // Call check() function for li element having class gfg2. $('li.gfg2').check(); </script> </body> </html> Output: When we use the function check() with list item with "gfg2" class that does not exist in the above code. When we use the function check() with list item with "gfg3" class that exists in the above code. Comment More infoAdvertise with us Next Article How to make jQuery throw an error when it doesn't match an element ? N nikhilchhipa9 Follow Improve Article Tags : Web Technologies JQuery jQuery-Propertie jQuery-Questions Similar Reads How to bind an event on an element but not on its children in jQuery ? In this article, we will learn how to bind an event on an element but not on its children in jQuery. We want to add an event on the parent element but not on the child element. Approach: We can do this task in the following steps: First, we add a click event on the div element that contains a paragr 2 min read How to Test if an Event Handler is Bound to an Element in jQuery (JavaScript)? To test if an event handler is bound to an element in jQuery, you can use a few simple methods. These help you check if an event listener is attached to an element, which is useful for debugging.Using .data() to Check Event HandlersYou can use the .data() method to check if any event handlers are as 2 min read How to checks the current selection against an expression using jQuery ? The task is to check the current selection against an expression using jQuery. jQuery is a lightweight and fast JavaScript that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. jQuery is widely famous for its motto of âWrite 2 min read How to check element exists or not in jQuery ? Checking if an element exists in jQuery involves selecting the element and verifying its length. If the selected element's length is greater than 0, the element exists in the DOM; otherwise, it doesn't. This is useful for conditionally manipulating elements. There are two ways to check whether an el 3 min read How to check whether the META key pressed when event is fired using jQuery ? jQuery is a feature-rich JavaScript library. It is a fast and most used JavaScript library. Before using jQuery you must have a basic knowledge of HTML, CSS, and JavaScript. In this article, we will learn about how you can check whether the META key was pressed when the event fired JQuery. META key: 2 min read Like