-1

The code is supposed to redirect the user to another internal page. However when the page loads with the code in it it automatically redirects the user without the user having to click the button.

I'm pretty new. I don't know what to try. I think it should work as is.

document.querySelector(".choice").addEventListener('apple', Redirect());

function Redirect()
{
    window.location = "challenge.html";
}

The expected result is that the user would click the text that contain the choice class and it would redirect the user to the challenge page.

The actual result is when the page loads it automatically redirects the user to the challenge page without the user having to click the button.

1 Answers1

0

You are invoking the event handler in this line:

document.querySelector(".choice").addEventListener('apple', Redirect());

Remove the parentheses:

document.querySelector(".choice").addEventListener('apple', Redirect);

You need to pass a reference to addEventListener otherwise with your current setup not only will the user be redirected immediately after the listener is registered, the button will also do nothing because the Redirect function is not returning something

dimlucas
  • 5,040
  • 7
  • 37
  • 54