-1

Sometime you see that we need event object in javascript.
Here is some code how can we pass event.

 function test(e){
  console.log(e); 
  e.preventDefault();
  console.log("you are not going to redirect");
 }

HTML

 <a href="www.google.com" onclick="test(event)">GOOGLE</a>

Just pass the event in function argument if you want to pass event in function.

Azeem Haider
  • 1,443
  • 4
  • 23
  • 41
  • Possible duplicate of [get the event object in an event handling function without pass the event object as parameters? (with jquery)](http://stackoverflow.com/questions/5849370/get-the-event-object-in-an-event-handling-function-without-pass-the-event-object) – Wesley Brian Lachenal Oct 22 '16 at 07:15
  • I want to use `e.preventDefault()` you can see the update question – Azeem Haider Oct 22 '16 at 07:24
  • your code semms correct, the prooblem is that your code does not show a code that goes allong with your function – youssouf Oct 22 '16 at 07:26

1 Answers1

0

setTimeout() does not pass an event object to its callback. And, even if you do find some way to get access to an event object in setTimeout(), it will be too late at that point to call e.preventDefault() because the default action will have already occurred.

So, your current approach to solving whatever problem you're trying to solve will not work. If you back up a few steps and explain the actual problem you're trying to solve, we could probably help you find a different way to solve it.

FYI, this type of question is known as the XY Problem where you describe a problem with your solution rather than the actual real problem you're trying to solve. An XY question vastly limits how we can help you.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979