Created
August 26, 2015 23:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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