-2

I have some ecommerce javascript that inserts additional html into my page once loaded. What I am trying to do is append that ecommerce html with more of my custom html. I have identified the specific ecommerce element that I want to append to and wrote the below code, but it doesn't seem to be able to locate the element. The code just continues to loop, even after the ecommerce html is loaded. What am I missing here.

var intervalId;
function checkForShopButton() {
intervalId = window.setInterval(updateShopButton, 500);
}

function updateShopButton() {
    if ($(".pricing").length) {
        clearInterval(intervalId);
        $(".pricing").append("<div>Review And Buy</div>");
    }
}

window.onload = checkForShopButton();
user1206480
  • 1,606
  • 2
  • 25
  • 43
  • what is the use of the setInterval? – madalinivascu Nov 26 '15 at 19:25
  • Show us your html, or even better provide a jsfiddle – Eric Guan Nov 26 '15 at 19:26
  • Downvotes, really? A div element with the class name of pricing is added to my page by the ecommerce javascript that is loaded. Since the element is not their initially as apart of my original html, I'm using the setInterval to continuosly check for the element until it is eventually inserted by the ecommerce javascript. – user1206480 Nov 26 '15 at 19:34

1 Answers1

0

Try this jquery hack

$(function(){
  $(".pricing").append("<div>Review And Buy</div>");
});
madalinivascu
  • 30,904
  • 4
  • 32
  • 50
  • The if statement never executes the true block as if the pricing class is never inserted into the page, but in fact it has been inserted. I'm just wandering if this has something to do with elements being inserted in general from javascript. – user1206480 Nov 26 '15 at 19:42
  • you are binding you function to the wrong event try binding it to the `DOMContentLoaded`, jquery uses this in the document ready event, see: http://stackoverflow.com/questions/9899372/pure-javascript-equivalent-to-jquerys-ready-how-to-call-a-function-when-the – madalinivascu Nov 26 '15 at 20:11