Your code doesn't work, because getElementById
returns a single HTML element, not an array-like collection of elements that has a length
property, which you can get.
Also, note that, since you're trying to get an element by its id, you'll end up having one element returned at all times anyway, or null
, if there's no element matching the given id.
For multiple grouped elements, try using classes and getElementsByClassName
or querySelectorAll
. Then, you can use length
on that.
Example Code:
// Check if an element with id 'frameBox' exists.
if (document.getElementById("frameBox")) {
//...
}
// Check if there are any elements with class 'frameBox'.
if (document.getElementsByClassName("frameBox").length) {
//...
}