You'd be better off doing this at the module level, rather than the theme level, as the JS will not take effect for admin pages otherwise (unless of course you're using the same theme for both).
Here's a small module to provide this functionality system-wide:
File: auto_upload.info
name = Auto Upload
description = Removes the need for users to press the 'Upload' button for AJAX file uploads.
core = 7.x
dependencies[] = file
File: auto_upload.js:
(function ($) {
Drupal.behaviors.autoUpload = {
attach: function (context, settings) {
$('form', context).delegate('input.form-file', 'change', function() {
$(this).next('input[type="submit"]').mousedown();
});
}
};
})(jQuery);
File: auto_upload.module
function auto_upload_init() {
drupal_add_js(drupal_get_path('module', 'auto_upload') . '/auto_upload.js');
}
Once you've installed the module all file inputs that are AJAX-ified (i.e. those that have an 'Update' button) will be affected...you won't need to press the 'Upload' button any more after selecting the file.
By using the delegate()
method this will also work perfectly for file fields that allow multiple uploads, and also for fields that are loaded into the page as the result of an AJAX request.
I've tested that in Chrome, Safari and Firefox and it works a treat :)
Footnote: In the (probably very unlikely) event that your site is using jQuery 1.7, you should use the on()
method, which has superseeded delegate()
.
UPDATE I've created a sandbox project for this module.