Skip to content

Instantly share code, notes, and snippets.

@scyt
Last active February 27, 2020 20:27
Show Gist options
  • Save scyt/0ef6f9e413686a2846390a5ccc45fd0e to your computer and use it in GitHub Desktop.
Save scyt/0ef6f9e413686a2846390a5ccc45fd0e to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Preview Image of File Upload
<div id="preview" style="display:none;">
<img src="" style="width:150px;">
</div>
/**
* Gravity Wiz // Gravity Forms // Preview Image of File Upload
*
* Outputs an image preview for File Upload fields.
*
* Based on: https://stackoverflow.com/questions/37880725/gravity-form-preview-of-image-upload
*/
( function( $ ) {
// Update "123" to Form ID and "1" to File Upload Field ID in "#input_123_1".
$( document ).on( "change","#input_123_1", function() {
readURL( this );
} );
function readURL( input ) {
if ( input.files && input.files[0] ) {
var reader = new FileReader();
$( '#preview' ).show();
reader.onload = function ( e ) {
$( '#preview>img' ).attr( 'src', e.target.result );
}
reader.readAsDataURL( input.files[0] );
}
}
} )( jQuery )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment