Then you need to make sure that the elements that you are referring to (changing/tieing changes to) exist before your code executes. The easiest way to do this is by checking that your elements exist with a Page Content fact in Audience. If you are doing changes to multiple elements you need to set a Page Content fact for each of these to be sure.
Notice that this might not be necessary if your content loads fast and all elements loads in at the same time. Then you can just set it to the element that is lowest in the HTML DOM.
In the example above we do changes to the buy button on the page so we set up a Page Content fact in our Audience for that element's CSS selector.