1

I want to check whether element attached to the DOM in javascript because I want to do some action when element gets attached to the DOM(on element attach event I want to do some action). Is there anything like addAttachHandler like GWT which is also present in javascript?

2

1 Answer 1

2

You can use MutationObserver which tells you when elements are added or removed from the DOM tree. It is fully supported by all modern browsers (IE11+).

The Elemento library (Apache 2.0 license) has a GWT JsInterop implementation of an attach/detach listener that uses MutationObserver behind the scenes. You might be able to draw inspiration from that code, or use it directly.

Sign up to request clarification or add additional context in comments.

2 Comments

Please add a code example rather than referring to links.
@Kyle Baker, I would normally show code, but the question doesn't actually ask for a code sample, and it'd take some time to put together a good example, so I tried to hint at the direction and leave it as "an exercise for the reader". It would be great if you can add an answer with code examples though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.