Last active
February 27, 2020 20:27
-
-
Save scyt/0ef6f9e413686a2846390a5ccc45fd0e to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Preview Image of File Upload
This file contains 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
<div id="preview" style="display:none;"> | |
<img src="" style="width:150px;"> | |
</div> |
This file contains 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
/** | |
* 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