Does jQuery have an exists() function to check if an element exists?
Hello, colleagues! I started learning jQuery and ran into a problem. I need to check if an element exists on the page before performing any actions with it. For example, I have this code:
```javascript
if ($('#myModal').exists()) {
$('#myModal').show();
}
```
But when I run it in the console, the browser throws an error: `Uncaught TypeError: $(...).exists is not a function`. I googled it and found that some people write something like `if ($('#myModal').length > 0)`. This does work, but it seems to me that an `exists()` method would be more logical and readable. Maybe I'm using the wrong version of jQuery? Or does this function need to be included separately? Please advise how to properly check for the existence of elements in jQuery? And is there a built-in method for this? I would be grateful.
No answers yet.
Log in to leave an answer