Drf
Drf

Reputation: 57

Parameter "e" in function

window.onkeyup = keyup;

var inputTextValue;

function keyup(e) {
  inputTextValue = e;
  $('#searchValue').text("https://duckduckgo.com/?q=" + inputTextValue);

  if (e.keyCode == 13) {
    window.location = "https://duckduckgo.com/?q=" + inputTextValue;
  }
}
  1. I dont understand this code. Does "window.onkeyup = keyup" mean that window.onkeyup will copy the value of the variable keyup?

  2. What does the parameter e do ? I don't see code that call the function and pass argument to that e parameter

Upvotes: 2

Views: 427

Answers (2)

Unmitigated
Unmitigated

Reputation: 89234

window.onkeyup = keyup; assigns the keyup function to the onkeyup property of the window. The function is then used as the event handler whenever a keyup event occurs on the window (or bubbles up to it).

The keyup event fires when the user releases a key that was previously pressed.

e is the event, which is, in this case, a KeyboardEvent.

According to MDN, for target.onkeyup = functionRef:

functionRef is a function name or a function expression. The function receives a KeyboardEvent object as its sole argument.

Upvotes: 2

Mahamudul Hasan
Mahamudul Hasan

Reputation: 2823

answer:

  1. when keyboard key will release then the keyup function will be called.
  2. e is the even object mainly e contain the keyboard related value,s example e.keyCode equal 13 for enter key.

so if (e.keyCode == 13) means when enter key will press and release then that block will be execute.

Upvotes: 1

Related Questions