Seems simple enough, and as far as I can see, I set it up right. But obviously not because it’s not working and I can’t seem to figure it out.
I’m adding it to a button, in order to trigger a Calendly embed popup.
Calendly had me add a snippet to the page, which I did. Then Calendly instructed me to trigger some additional code onClick, which I’m using the above method for.
Hi Sachin, thanks for replying! So move this script:
<script>
window.onload = function() {
var anchors = document.getElementsByTagName('*');
for(var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function() {
code = this.getAttribute('whenClicked');
eval(code);
}
}
}
</script>
from the head section to the"Before tag"? section?
Or did you mean the Calendly script that I current have in the attribute section of the button element?
If you’re talking about the script I have in the attribute section, if I added that before the /body section, would I need to target the button too? With something like this?:
Hi, just want to point out that the whenClicked snippet (from 2016) is using eval() that is usually considered dangerous and best avoided so you don’t accidentally allow malicious code injection on your site, see eval() - JavaScript | MDN!
I was able to add a custom onClick just by using the Custom Code settings. Set the ID of your element then add something like this to the Footer code.
<script type='text/javascript'>
document.getElementById("nav-contact-button").onclick = function() {
//do something, for example
CustomContactPopup.show();
}
</script>
I have multiple buttons all with the same ID that i would like to trigger towards the same function. It seems this solution only works to the first ID it finds but not others on the same page. Do you have any idea on how to solve that?