How to check if an element exists in javascript?
JavaScript 1: find element by id
// find elements by id
document.querySelectorAll('#element_id').length > 0) {
// Do something
}
JavaScript 2: find element by class
// find elements by element class
document.querySelectorAll('.element_class').length > 0) {
// Do something
}
-
CreatedJan 16, 2020
-
UpdatedNov 03, 2020
-
Views1,390
Related Articles
Check/Uncheck checkboxes when parent checkbox is checked/unchecked and vice-versa in Java Script
Set DropDownList value based on text/value in Java Script
Get selected text/value from DropDownList (or) Select in Java Script
Get selected index from DropDownList (or) Select in Java Script
How to validate number in Java Script
How to add or remove rows dynamically to HTML Table in Java Script
How to create HTML Table dynamically in Java Script
How to get or set value of a textbox in Java Script
How to get Key Codes in Java Script
How to replace string in Java Script?