Help me with an onClick snippet?

Hi, I found this thread that shows a method to enable adding an onClick event to a Webflow element: Add onClick attribute to any element with this JS snippet

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.

Calendly instructions:

Any help would be greatly appreciated, I promise I’ve worked on it for a while, and just can’t seem to get it working.

Here’s my read-only link: Webflow - EVOLVEXGUARDDIGITAL

Thanks a bunch.

Hey, @jackwabbit try pasting your whenClicked script Before </body> tag and can you add your published .io link so that we can look.

1 Like

Hi Sachin, thanks for replying! So move this 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');

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?:

document.getElementById("cta").onClick ='Calendly.showPopupWidget('');return false;';

And here is the .io link:

Sorry for my slow response, stepped away to eat some dinner.

Hi community. Shameless bump :sweat_smile:

I think I made it a bit confusing in my reply to @Sachin sorry for that.

Anybody else have any ideas on how I might be able to get what I described in the original post working?

@benavnon not sure if you’re still active on the forum or not, but do you know if the method in your thread I linked to in OP is still working?

Hey got it working. Had to remove return false; from the end of the Calendly snippet.

1 Like

FINALY - Thank you for posting the solution, I was going crazy trying to trigger Calendly from a button.

Btw, did you use a button element or did you style a div as a button?

Wow man… You saved me a lot of time too! Thanks Jack!!

Daniel, I used a button on my side.

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;

I found a way! :tada: You can workaround the onclick attribute using addEventListener. Check the following documentation:

Here’s the example:

const element = document.getElementById("myBtn");
element.addEventListener("click", myFunction);

function myFunction() {
  alert("Hello World");