Making sure element(s) exist before changes are made

To make sure the element(s) that you are doing javascript/jQuery changes exist before the code is run you can add these as Page content Audience statements. This will ensure that the project code isn't executed before the element exist. It will be executed as soon as the Audience becomes true.


In the example above I check that the Buy button exists by adding a Page content statement and setting the Buy button's CSS selector as the value.

I can now do changes to this element in my code without worrying about it being executed before the element has been loaded/exist.

For example, change the copy of this button:

document.querySelector('.buy-btn').innerText = 'Buy now';

Notice that if you have multiple elements that need to exist before your code executes you need to add separate statements for each to be sure that all exist before the project code executes (on Audience true)

Was this article helpful?
0 out of 0 found this helpful