Trigger Live Chat through JS/JQuery

forms

#1

Hi,

I have a button on my homepage. This button needs to simulate if a user clicks on the live chat button.

Is there a way that I can make the live chat expand on a button click?

Thanks!
Toby


#2

Looks like they don't have any public APIs for the live chat widget at the moment.


#3

Hello everyone!

We have found a very easy solution. Watch the video to see the solution.

You can use the Javascript code below:

// logo-btn is ID for the element assigned with click event to show Hubspot chat window

// jQuery (ES6)
$('#logo-btn').on('click', () => {
  // check if Hubspot chat is installed
  if (window.hubspot_live_messages_running && window.hubspot && window.hubspot.messages) {
    window.hubspot.messages.EXPERIMENTAL_API.requestWidgetOpen();
  } else {
    console.log('Please install HubSpot WordPress plugin');
  }
});

// VanillaJs (ES5)
document.getElementById('logo-btn').addEventListener('click', function(event) {
  // removef if it is no need to prevent default event such as url redirect in the <a> element
  event.preventDefault(true);
  // check if Hubspot chat is installed
  if (window.hubspot_live_messages_running && window.hubspot && window.hubspot.messages) {
    window.hubspot.messages.EXPERIMENTAL_API.requestWidgetOpen();
  } else {
    console.log('Please install HubSpot WordPress plugin');
  }
});