Joel Min Joel Min - 2 years ago 65
Javascript Question

Why is my JS function executed onload instead of onchange?

Here's the code:

window.onload = function() {
oldonload && oldonload();

function test() {


The code is fairly straight forward,
is called only when
is changed. However
is being called whenever my page is loaded.

What is going on here? How would I execute the function only when user interacts with the input element?

Answer Source

You are calling the method on this line:


Change it to:


If you want to pass a parameter to test:

var myVar = 5;
$('input[type=file]').change(function () {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download