0

I am trying to exceute a javascript function after another div that has been popupated by javascript has loaded. The div has been populated first with javascript is '#am-events-booking'. The function i am trying to use is:

$(window).load(function ()
{
  var i = setInterval(function ()
  {
    if ($('#am-events-booking').length)
    {
        clearInterval(i);
    }
  }, 1000);
  alert('Page is loaded');
});

2 Answers 2

1

I always use $(document).ready() to run code after the page has loaded. Not sure what the difference is, but at least then it works.

Furthermore you need to use .text() to get the text inside an element.

Working code snippet:

$(document).ready(function() {
  var i = setInterval(function() {
    if ($('#am-events-booking').text().length) {
      clearInterval(i);
      console.log('Text detected!');
    } else {
      console.log('Waiting...');
    }
  }, 1000);
  console.log('Page is loaded');
});

setTimeout(loadText, 2200);

function loadText() {
  $('#am-events-booking').html("<h2>Hello</h2>");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="am-events-booking"></div>

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

Comments

0

$(document).ready(findDiv);
    
function findDiv() {
  if($('#am-events-booking').is(':visible')){ // if the div is visible
     alert('Page is loaded'); 
  } else {
  console.log("loading...");
    setTimeout(findDiv, 50); // wait 50ms, 
  }
}

$('body').html('<div id="am-events-booking"></div>');
<body></body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

you can use is :visible form JQuery to check if div is added or not

Comments

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.