I hope this question is not duplicated.
I have two HTML elements, one which when it's changed must populate the value of another via an async method named foo().
The following code works:
<input type="text" id="elem">
<input type="file" onchange="
(async ()=>{
document.getElementById('elem').value = await foo(this);
})()
">
The following one doesn't (no exception is thrown in console, and #elem is not updated):
<input type="text" id="elem">
<input type="file" onchange="
async ()=>{
document.getElementById('elem').value = await foo(this);
}
">
Why the first example works and the second doesn't?
(function() {...})() This invokes the function.
it's like invoking a named function someFunction()
in your second example you're just creating a function without invoking it. it returns the function itself.
onchange='someFn()'; // calls function
onchange='someFn'; // returns function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With