[javascript] Keyup event and change event questions

Asked 2 years ago, Updated 2 years ago, 102 views

Hello, the code in the link below is to find out if there is a city with the word you entered and to print it out on the screen. https://codepen.io/w00kgod/pen/XGbpwy

After the input occurs in the input tag, to find out if the input value is included in the array containing the city's information

input.addEventListener('change',function(){
  const matchedArray = FindMatchesArray(input.value,cities);
  displayMatchedArray(matchedArray);
});

I received a change event as shown in . In this case, the screen does not print out after you input it I had to click somewhere else with my mouse. Question 1: I think it is because I only input it with a keyboard because I don't think it's a change event. What event does the change event use to handle? In this case, it seems right to receive and handle the keyup event, not change. It is said that the change event is mainly used when there is a change in input. Among the changes, most of them seem to be changes that can be handled by keyups, so I'm curious about the cases where I usually use them.

Therefore, we added a keyup event so that the output occurs every time a letter is entered.

input.addEventListener('keyup',function(){
  const matchedArray = FindMatchesArray(input.value,cities);
  displayMatchedArray(matchedArray);
});

Question 2: Why is the value of input.value stored after the keyup event occurs? Why is input.value saved in keyup event?

javascript keyup change event

2022-09-22 18:13

1 Answers

As I filled out the question, I got to know the answer 2 times. input.value stores the value in value when input occurs in input regardless of the event type. However, the stored value is delivered asynchronously to the client, so it should be delivered as a function such as addEventListener.

Also, the change event is said to operate outside of input. Therefore, if you write code with a change event, you must mouse over the outside of the input.


2022-09-22 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.