0

I know I can access dynamic elements using an on click event like this:

$('body').on('click', '.el', function() { ...

but how can I get .el on document load?

I tried for example:

$(document).ready( function() {
 var test = $('.el').length;
})

But it returns 0.

.el is loaded onto the page dynamically.. when the document loads. It is loaded through another js file which i don't have access to.

CyberJunkie
  • 21,596
  • 59
  • 148
  • 215
  • $(window).load() fn i guess because it triggers on loading is completed – MJN Jan 25 '19 at 17:25
  • 2
    I think you need to add the `.`, otherwise it looks for `` elements on the page: `var test = $('.el').length;` – chrisg86 Jan 25 '19 at 17:27
  • 1
    If `.el` is being loaded through another process, you can't know if it's there on document ready or window load or sometime after that. You either need to poll (check for its existence every second after document ready), wait a sufficiently long time (whatever that is), or use something like [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to watch the DOM and alert you when the element is added. – Heretic Monkey Jan 25 '19 at 17:32

0 Answers0