How to check existence of element by id using Mootools
<div id="foo">some content</div>
var foo = document.id('foo'); // or $ but should be avoided due to conflicts
// if it returns an Element object, it will be truthy.
if (foo) {
// code for when it exists
}
else {
// code for when it does not.
}
incidentally, this mimics the behaviour of the return value of document.getElementById
which is vanilla js. it can be true for any selector that is meant to return a single Element, like document.getElement('div.login > a.active')
- does not need to be by ID only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With