php - Javascript: Image preview is not working correctly -
i explain first code
i have 2 views make preview of image (input file).
view have form , make preview of inputs file:
create project view:
<table> <tr> <td> <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;" onerror="this.src='data:image/png;base64,ivborw0kggoaaaansuheugaaaaeaaaabcaqaaac1hawcaaaac0leqvr42mnkyaaaaayaajcb0c8aaaaasuvork5cyii='"> <input type="file" name="pathheader" id="pathheader" class="form-control-file" aria-describedby="filehelp" style="display:none;"> </td> <td> <img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;" onerror="this.src='data:image/png;base64,ivborw0kggoaaaansuheugaaaaeaaaabcaqaaac1hawcaaaac0leqvr42mnkyaaaaayaajcb0c8aaaaasuvork5cyii='"> <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="filehelp" style="display:none;"><br> </td> </tr> <tr> <td> <input type="button" name="" value="seleccionar header" id="browse_file" class="btn btn-danger form-control"> </td> <td> <input type="button" name="" value="seleccionar home" id="browse_file2" class="btn btn-danger form-control"> </td> </tr> </table> edit project view:
<table> <tr> <td> @if (storage::disk('projects')->has($project->slug)) <img src="{{ asset('/storage/projects/'.$project->slug.'/header.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @else <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @endif <input type="file" name="pathheader" id="pathheader" class="form-control-file" aria-describedby="filehelp" style="display:none;"> </td> <td> @if (storage::disk('projects')->has($project->slug)) <img src="{{ asset('/storage/projects/'.$project->slug.'/home.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @else <img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @endif <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="filehelp" style="display:none;"><br> </td> </tr> <tr> <td> <input type="button" name="" value="seleccionar header" id="browse_file" class="btn btn-danger form-control"> </td> <td> <input type="button" name="" value="seleccionar home" id="browse_file2" class="btn btn-danger form-control"> </td> </tr> </table> javascript code:
$("#browse_file").on('click',function(e){ $("#pathheader").click(); }); $("#browse_file2").on('click',function(e){ $("#pathhome").click(); }); $("#pathheader").on('change',function(e){ var fileinput=this; if (fileinput.files[0]) { var reader=new filereader(); reader.onload=function(e) { $("#img").attr('src',e.target.result); } reader.readasdataurl(fileinput.files[0]); } }); $("#pathhome").on('change',function(e){ var fileinput=this; if (fileinput.files[0]) { var reader=new filereader(); reader.onload=function(e) { $("#img2").attr('src',e.target.result); } reader.readasdataurl(fileinput.files[0]); } }); when select image on create project view, code works, make preview of file.
problem
when edit project , select 2 files see preview of new image in pathheader not in pathhome.
why preview of new image on pathhome isn't working? check errors , don't display one.
thanks lot, , if need more information or think isn't clear make me known , try explain better.
you have typo in html code , maybe that's why javascript isn't able find element
$("#img2").attr('src',e.target.result); <td> @if (storage::disk('projects')->has($project->slug)) <img src="{{ asset('/storage/projects/'.$project->slug.'/header.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @else <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @endif <td> @if (storage::disk('projects')->has($project->slug)) <img src="{{ asset('/storage/projects/'.$project->slug.'/home.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @else <img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;"> @endif <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="filehelp" style="display:none;">
Comments
Post a Comment