Can I run jQuery function and on load and on change?

Braggae picture Braggae · Jul 29, 2013 · Viewed 10.1k times · Source

I want to run a script on page load first time (for default value of select element), and then on each change of select element. Is it possible to do it in one line, or do I need to duplicate function for this events?

I know that I can use next structure:

$('#element').on('keyup keypress blur change', function() {
    ...
});

But it doesn't seem to support load event.

Any ideas?

Answer

A. Wolff picture A. Wolff · Jul 29, 2013

If i understand what you mean, just trigger any one of these events:

$('#element').on('keyup keypress blur change', function() {
    ...
}).triggerHandler('keyup'); //or trigger('keyup') or keyup() which then let event propagate