Skip to main content

Posts

Showing posts from December, 2014

JavaScript: How to Detect if an Element Exists?

There are events in JavaScript as reference before we start to capture the DOM element: load or DOMContentLoaded or readystatechange , etc. But sometimes, we can't solely depend on either one of those events to detect when the elements have been ready (available) for further DOM manipulation. Especially if the element is injected dynamically . So to answer that, we can use setInterval to wait and repetitively search the particular element(s) we need to find. Using setInterval Let's wrap it with anonymous auto-invoked function . (function (d) { let interval // For keeping "setInterval" method. // Function to check the element. function check() { // The element we need to find. const elementWeNeedToDetect if (elementWeNeedToDetect) { console.log("Element is found"); // Attach a callback or directly do something here. clearInterval(interval) // Stop timer. ...
Monkey Raptor uses cookies or biscuits 🍪 for analytics, functionality, and advertisements. More info in Privacy Policy