I want to receive a return value from the function specified in the addventlistner.

Asked 1 years ago, Updated 1 years ago, 20 views

We are unable to receive the return value of the function with the following code:When an event occurs, I want to run the function and receive the return value, but I can't think of a way to receive the return value.I'm an amateur, so I might be wrong with my idea.Professor, please.

<script>
function test(e){
    var hoge = 1;
    return1;
}
document.addEventListener('change', test, false);
</script>

javascript

2022-09-30 16:07

2 Answers

Unfortunately, as you said, the idea itself is wrong.The function specified in addEventListener cannot receive a return value.
Instead of returning the return value with the function specified in addEventListener, you must use the value (in test in this example) to perform the action.

If you don't know what to do, you may be asked a more specific situation and a solution may be answered.


2022-09-30 16:07

The listener function is an asynchronous function, so the return statement does not accept the return value.
JavaScript requires the completion of asynchronous processing to be received by the callback function.

'use strict';
function handleChange(event) {
  foo(event.target.value);
}

function foo(v){
  console.log(v);
}

document.addEventListener('change',handleChange,false);
<select><option value="1">1>/option value="2">2>/option value="3">3>/option value="3>3>/option>>>>4>>>>4>>>>4;

Dear Re:Sogeking,


2022-09-30 16:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.