We can make use of the .length property in jQuery to know if an element exists or not in the HTML page DOM.
The .lenght property returns a count of the number of elements that are present on the page. If there is no match then the count is zero.
Example:
<div id="welcome">
Welcome to Code2care.org
</div>
<div id="tagline">
Code2dcare - Lines of code for a change!
</div>
<script>
if ($('#welcome').length > 0) {
console.log("Element exists.");
} else {
console.log("Element doesn't exist.");
}
</script>
Output:

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to jQuery,
- How to check if an element is hidden using jQuery code?
- Disable jQuery button after being click
- Make div element draggable using jQuery
- Get Browser Screen Width and Height dynamically using jQuery
- jQuery : Move to top of the page
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5
- Redirect page using jQuery
More Posts:
- How to Right Click on Mac Desktop? - MacOS
- PHP drag and drop file upload tutorial using dropzone.js - PHP
- How to uninstall/remove or disable Microsoft Teams - Teams
- Setup synonyms or alias or thesaurus in SharePoint - SharePoint
- How to add Back Button on Toolbar in Android [Tutorial] - Android
- [Solved] Mic not working on iPhone 7 after iOS 14 upgrade - Apple
- 12 August - International Youth Day celebrated worldwide - News
- How to make jsfiddle bootstrap ready - CSS