+ 3

How to fire a javascript Event on file select?

So basically, i have ``` <input type="file" id="infile"> ``` And now I want to fire a javascript event when a file is choosed using it. I tried: ``` document.getElementById('infile').addEventListener('change', somefunc()); ``` But ```somefunc()``` is being executed ```onload```! Please help!!

29th Jul 2020, 6:41 AM
Gyan Prakash Singh Jaruhar
Gyan Prakash Singh Jaruhar - avatar
3 Answers
+ 5
You need to get rid of the brackets after somefunc. E.g. ``` document.getElementById('infile').addEventListener('change', somefunc); ``` Because otherwise you are executing somefunc.
29th Jul 2020, 6:43 AM
Rowsej
Rowsej - avatar
+ 4
Thanks Rowsej, It worked !!!
29th Jul 2020, 6:51 AM
Gyan Prakash Singh Jaruhar
Gyan Prakash Singh Jaruhar - avatar
+ 4
No worries!
29th Jul 2020, 6:54 AM
Rowsej
Rowsej - avatar