The input element has a change event that fires when the form field changes. So:
$('selector_for_your_form').delegate('input[type=file]', 'change', function() { var form = $(this).closest('form'); form.append(); });
Uses delegate , which means you donโt need to explicitly bind the event to a new element.
Real-time example (I assume your markup will be a little more interesting - and certainly looks better - than shown there)
Update . Unfortunately, change does not start immediately in some browsers (it waits until the user distracts the focus from the field in IE7, for example). jQuery handles this weirdness for you if you are attaching an event directly but not using delegate . So here is an alternative:
$('selector_for_your_form input[type=file]').change(fileChangeHandler); function fileChangeHandler() { var form = $(this).closest('form'); $('<input type="file">').change(fileChangeHandler).appendTo(form); }
If your markup is more complex than input itself (and I assume it is), you need to be sure that you are connecting change to the right element.
Living example
Tj crowder
source share