I am trying to apply the code below to navigate. but the problem is, it is not working on a single click. it starts working on second click. plase help me to understand the problem for not working on first click.
function handlePurchaseBtn() {
document.getElementById('go-home-btn').addEventListener('click', function() {
window.location = 'https://www.google.com'
})
}
Should be the other way around - if that's the desired:
function handlePurchaseBtn(evt) {
evt.preventDefault(); // If needed, to prevent a default browser action
window.location = "https://www.google.com";
}
document.getElementById("go-home-btn").addEventListener("click", handlePurchaseBtn);
With the above solution make sure to remove the inline HTML attribute onclick="handlePurchaseBtn()" from your button:
<button id="go-home-btn" type="button">Go home</button>