I have to buttons, each with an onclick function but I want to merge them into 1.
Button 1: Show a message that things are ok
<button type="button" onclick="UIkit.notification({message: 'This is an alert'})">Click me</button>
Button 2: Add a product to cart (WooCommerce)
<a href="javascript:void(0);" onclick="window.location.href='https://chatsales.nl/pakketten/doe-het-zelf?add-to-cart=420'">Click me</a>
How can I merge those 2 onlick actions into 1 single button click?
Many tnx.
PS. This doesn’t seem to work (product gets added but message is not shown):
<script>
function action1(){
UIkit.notification({message: 'Notification message'});
}
function action2(){
window.location.href='https://chatsales.nl/pakketten/doe-het-zelf?add-to-cart=420';
}
</script>
<button type="button" onclick="action1();action2();">Click me</button>
3
Answers
You could just create a new function where you execute both actions. Then pass this new function to your button.
You can’t assign more than one handler to
.onclick
, but you can do it with.addEventListener(event, handler)
like this:You can do the same with one click only like