Last active
May 16, 2016 15:45
-
-
Save emsifa/1a98f1b6301439faa7706691732f36b8 to your computer and use it in GitHub Desktop.
Simple jQuery plugin untuk preview image upload (demo: https://jsfiddle.net/emsifa/buxudv6t/3/)
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
(function() { | |
var previewImage = function(input) { | |
var $previewTarget = $($(input).attr('preview-to')); | |
var $previewImage = $previewTarget.get(0).tagName == 'IMG'? $previewTarget : $previewTarget.find("img"); | |
if (input.files && input.files[0]) { | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
$previewImage.attr('src', e.target.result); | |
$previewTarget.show(); | |
} | |
reader.readAsDataURL(input.files[0]); | |
} else { | |
$previewTarget.hide(); | |
} | |
} | |
$.fn.previewable = function() { | |
var $this = this; | |
return $this.each(function() { | |
previewImage(this); | |
$(this).change(function() { | |
previewImage(this); | |
}); | |
}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment