How to track button click in salesforce using javascript

188 Views Asked by At

I am new to salesforce and javascript.
I have a button on a page and want to record the button if it gets clicked on the page to the customer profile as a boolean.

I have a button with an id="example-button".

I have a function in a scripthelper.js file that goes like function

function addedToCart(){
  var addedToCart = customer.getProfile.getCustom().buttonAdded; //Gets the custom attribute from the Business Manager
  var buttonClicked = document.getElementById("example-button");//grabbing button element
  if(buttonClicked)// this is where I dont know the condtional 
    addedToCart  == true;
  }
}

I don't know what to add in the if statement to check if the button was clicked so the custom attribute can be set to true. I know I am using front end with the get element by ID, so if there another or easier way to do this please let me know!

If the button is clicked I want the attribute to be set to true

1

There are 1 best solutions below

0
Maher Nabil On

You need to set a tracking event on the targetted button:

$('#example-button').on('click', function (e) {

});

Check this for future reference: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events