![]() |
VOOZH | about |
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:
Output: