Skip to content

Perform Function when Element ID is Visible

I’m attempting to perform a function after an element ID becomes visible on a page. I want to make it so that when an element with the ID #discountbox becomes visible, then peform this function.

This is the javascript I’ve created so far:

jQuery(document).ready(checkContainer);

function checkContainer () {
  if($('#discountbox').is(':visible'))){ //if the container is visible on the page
    $("#discountbox").clone().insertAfter(".discount-summary");
  } else {
    setTimeout(checkContainer, 50); //wait 50 ms, then try again
  }
}

Unfortunately, this does not appear to be working.

Answer

I’d tried you code here, and it works.

  • what only i changed is just remove one ‘)'( maybe you alreay knew )
    if($('#discountbox').is(':visible')))

to

    if($('#discountbox').is(':visible'))