Show Image Preview Before Upload In jQuery Bootstrap Example


Today, Laravelcode share one helpfull tutorials about jquery and bootstrap. in this tutorials we are show you how to show image preview before upload.

We are need some time this type functionality in frontend site which you want to show image preview before it upload in server. resently we are working on one laravel application and we are need this so we are make one simple script for it using jquery and bootstrap. and we are finaly done this with jquery and bootstrap.

Here, we are always share our code and example which we are facing in our devloping mode and when we are find finaly solution about it the we are also share with you.

You can try this example with bootstrap because we are using bootstrap for fine layout.

Step : 1 HTML code look like

<!DOCTYPE html>
	<title>Show Image Preview Before Upload</title>
	<link rel="stylesheet" type="text/css" href="">
	<script src=""></script>
	<script src=""></script>
	<div class="container">
	    <div class="row">    
	        <div class="col-xs-12 col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">  
	            <div class="input-group image-preview">
	                <span class="input-group-btn">
	                    <button type="button" class="btn btn-default image-preview-clear" style="display:none;">
	                        <span class="glyphicon glyphicon-remove"></span> Clear
	                    <div class="btn btn-default image-preview-input">
	                        <span class="glyphicon glyphicon-folder-open"></span>
	                        <span class="image-preview-input-title">Browse</span>
	                        <input type="file" accept="image/png, image/jpeg, image/gif" name="input-file-preview"/>

Step : 2 CSS code look like

Then add some css code copy following code and add in your css file.

.image-preview-input {
    position: relative;
	overflow: hidden;
	margin: 0px;    
    color: #333;
    background-color: #fff;
    border-color: #ccc;    
.image-preview-input input[type=file] {
	position: absolute;
	top: 0;
	right: 0;
	margin: 0;
	padding: 0;
	font-size: 20px;
	cursor: pointer;
	opacity: 0;
	filter: alpha(opacity=0);
.image-preview-input-title {

Step : 3 jQuery code look like


The finaly add following jQuery code in js file.

$(document).on('click', '#close-preview', function(){ 
    // Hover befor close the preview
        function () {
         function () {

$(function() {
    // Create the close button
    var closebtn = $('<button/>', {
        text: 'x',
        id: 'close-preview',
        style: 'font-size: initial;',
    closebtn.attr("class","close pull-right");
    // Set the popover default content
        title: "<strong>Preview</strong>"+$(closebtn)[0].outerHTML,
        content: "There's no image",
    // Clear event
        $('.image-preview-input input:file').val("");
    // Create the preview image
    $(".image-preview-input input:file").change(function (){     
        var img = $('<img/>', {
            id: 'dynamic',
        var file = this.files[0];
        var reader = new FileReader();
        // Set preview image into the popover data-content
        reader.onload = function (e) {

Now we are ready to run our example so run bellow command ro quick run:

If you face any problem then please write a comment or give some suggestions for improvement. Thanks...