Skip to content

Instantly share code, notes, and snippets.

@christierney402
Created August 26, 2015 23:08
Show Gist options
  • Save christierney402/2ca3c2352bc4645bcecf to your computer and use it in GitHub Desktop.
Save christierney402/2ca3c2352bc4645bcecf to your computer and use it in GitHub Desktop.
Use an element to get a file dialog and upload instead of a file input element
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<style>
input {
display:none;
}
</style>
<form method="post" enctype="multipart/form-data">
<div id="wrapper" style="background-color:red;">click here to upload<br><br>
<input type="file">
</div>
</form>
<script>
var $fileUploadInputWrapper = $('<div/>').css( {height:0, width:0, 'overflow':'hidden'} );
$(':file').wrap($fileUploadInputWrapper);
// to use stopPropagation instead if statement use:
// $('#wrapper > :file').click(function(event) { event.stopPropagation(); });
$('#wrapper').click(function(event){
if( !$(event.target).is(':file') ) {
$(':file').click();
}
});
$(':file').change( function() {
$('form').submit();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment