Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check existence of element by id using Mootools

Tags:

mootools

How to check existence of element by id using Mootools

like image 620
Aadi Avatar asked Jan 21 '23 18:01

Aadi


1 Answers

html:

<div id="foo">some content</div>

javascript

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.

like image 104
Dimitar Christoff Avatar answered May 05 '23 22:05

Dimitar Christoff