jQuery on Button/Toggle Click

Hey there :slight_smile: I trust you’re having an awesome week!

I’m running the latest versions of WP and Pro. I have a Content Area Off Canvas element with class "begin-chat". I’m wanting to execute some code when the toggle is clicked. So I have the below…

jQuery(function($){
  $(document).on('click', .begin-chat', function(){
    console.log('do something here');
  })
})

However, the code never fires… I don’t see anything in the log. it seems that the theme has preventDefault() and maybe even stopPropogation() on toggle clicks… Is that accurate? Either way, is it possible to run some code on the toggle click?

Thanks heaps for your time! Conor

Hi Conor,

Thanks for writing in. Yes, it is possible to run some code on the toggle click. I took a look at your code, it seems that there’s a missing single quotation before the .begin-chat selector.

I updated your code, try using this.

jQuery(function($){
  $(document).on('click', '.begin-chat', function(){
    console.log('do something here');
  })
})

Hope it works.

Thanks for the reply @albrechtx :slight_smile:

Turns out that was a typo just here in the forums. It was accurate on my site, so the code still doesn’t work for some reason. I’ll post a link to the page below, with login details if that’s helpful.

I’m sure I’ve done this a million times before… So not sure what’s going on. I’ve just disabled all plugins to check for conflicts, and it still doesn’t work. Rather frustrating!

EDIT: Forgot to say, it’s the button at the bottom of the page which says “Begin The Conversation”

Hello @conorseed,

Your JS code should be updated and use this code:

jQuery(function($){
  $('#begin-chat-anchor-toggle').on('click touchstart', function(){
    console.log('do something here');
  });
});

The action will be trigger on click on desktops and on tap in mobile and touch devices.

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.