Search code examples
javascriptjquerysquarespace

Trigger button click from other button


I'm trying to trigger a button click from another button to use in my squarespace website.

I'm adding the following to the header:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        offerteHeader = $( "a.theme-btn--primary-inverse:contains('Offerte aanvragen')" );
        offerteForm = $("button:contains('Offerte aanvragen')");
        offerteHeader.removeAttr("href");
        offerteHeader.on( "click", function() {offerteForm.click()});
    });
</script>

When I click the OfferteHeader button nothing happens :(.

OfferteHeader and offerteForm seem to refference fine when I log the to the console.

Also firing offerteForm.click() from the chrome dev console seem to work fine and trigger the event.

I also checked if the listener was working with a log to the console and if it could refference to offerteForm.

What is the problem here? The site can be found on https://sturgeon-groundhog-cnzt.squarespace.com/ password is bellboy. Sorry, it's a squarespace site, can not change that.


Solution

  • this isn't best practice but working:

    $(document).ready(function() {
      var offerteHeader = $("a.theme-btn--primary-inverse:contains('Offerte aanvragen')");
      var offerteForm = $("button:contains('Offerte aanvragen')");
      offerteHeader.click(function(e) {
        setTimeout(function() {
          offerteForm.trigger("click")
        });
      });
    });