Trigger a function on button

Hello, i want to link JS function to button. I tried several ways, but i can’t make it work. Function works with onclick attribute on button, but i dont know how i can add it on webflow. Thanks!

<script>

  var amount = 1;
  
  function more()
  {
    amount++;
    document.getElementById('amount-div').innerHTML = amount;
  }

</script>

Hey,

you can try using an eventlistener instead:

var amount = 1;
const btn = document.getElementById('amount-div');

btn.addEventListener('click', function() {
    amount++;
    btn.innerHTML = amount;
});

Sorry, I messed up somewhere. I created an example with this code instead:

<script>

let amount = 1;

const btn = document.getElementById('btn');
const amount_div = document.getElementById('amount-div');

btn.addEventListener('click', function() {
	amount++;
  amount_div.innerText = amount;
});

</script>

https://btn-example.webflow.io

https://preview.webflow.com/preview/btn-example?utm_medium=preview_link&utm_source=designer&utm_content=btn-example&preview=a148242f260209c3dce4b4ab718eda5b&workflow=preview

Works perfectly! thanks Buddy :slight_smile:

1 Like