Robus Robus - 3 months ago 9
jQuery Question

When does the jquery change function trigger?

Referring to this post: Jquery execute onchange event on onload

I got the opposite Problem.

This was my first attempt but the function executes on page load

I excpected it to trigger on the first change.

function get_items(){
alert("hello");
};
$('#select_field').change(get_items());


This one works and there is only an alert if i really change the select_field:

$('#select_field').change(function(){
get_items();
});


It is probably some basic stuff but i cant find anything helpfull.
Can someone pls explain whats happening here?

Thank you

Answer

$('#select_field').change(get_items()); should be $('#select_field').change(get_items);

Binding event handlers requires passing a function reference. When you call get_items() you're executing the function.