Ernest
Ernest

Reputation: 824

How to detect if DOM has finished build?

What jQuery/JavaScript command can I use to detect whether the DOM (driven by a custom function) has been built before populating the fields inside it? Currently the latter fails unless I put in an alert box in between the 2 actions.

Upvotes: 3

Views: 1357

Answers (2)

McKayla
McKayla

Reputation: 6949

You can look at document.readyState. If the page is ready it should be "complete".

You can also use document.addEventListener('DOMContentLoaded', function () { })

Upvotes: 3

alex
alex

Reputation: 490223

$(function() { }) will tell you when the DOM is ready to be accessed.

If your custom function is adding things to the DOM, the best way would be to build something into it that notifies some code when it is done.

Upvotes: 6

Related Questions