maddy maddy - 1 month ago 10
jQuery Question

Jquery Preview image before upload

i need to preview image before upload i tried following code but it's not doing anything.

<form name=”user_profile” id=”user_profile” action=”” method=”POST” enctype=”multipart/form-data” >

<div class=”profile”>
<img class=”list_prev” id=”user_old” src=”add path her for preview image from folder”>

<input type=”file” id=”user_image” class=”image ” name=”user_image” value=”” onchange=”preview_profile_image(this)” />

</div>

</form>



This is my script..


<script type=”text/javascript”>

function preview_profile_image(input) {
if (input.files && input.files[0]) {
var filerdr = new FileReader();

filerdr.onload = function(e) {
jQuery(‘.list_prev’).attr(‘id’, e.target.result);
}

filerdr.readAsDataURL(input.files[0]);
}
}

</script>



anyone please help me what's wrong.

Answer

Use src instead of id at line jQuery(‘.list_prev’).attr(‘src’, e.target.result);