0

If i won't use div for input field the image will get changed as shown in first image. But, i don't want to display that input field as choose file, so i wrote css for that input field. Can any one help me out from this?

HTML CODE

if no divison for input field, image will get updated.

I don't want to display chhose file for input so i added css but it's failed to work properly

<div class="row">
<div class="col-md-3">
    <div id="changepic">
        <i class="fa fa-camera"><input type='file' class="imgInp fileInput" />  Change Photo</i>
   </div>
   <img class="blah" src="garments.jpg" width="150px" height="150px" alt="your image" />
</div>
<div class="col-md-3">
    <div id="changepic">
        <i class="fa fa-camera"><input type='file' class="imgInp fileInput" />  Change Photo</i>
   </div>
   <img class="blah" src="garments.jpg" width="150px" height="150px" alt="your image" />
</div> 
<div class="col-md-3">
    <div id="changepic">
        <i class="fa fa-camera"><input type='file' class="imgInp fileInput" />  Change Photo</i>
   </div>
   <img class="blah" src="garments.jpg" width="150px" height="150px" alt="your image" />
</div> 
<div class="col-md-3">
    <div id="changepic">
        <i class="fa fa-camera"><input type='file' class="imgInp fileInput" />  Change Photo</i>
   </div>
   <img class="blah" src="garments.jpg" width="150px" height="150px" alt="your image" />
</div>                              
</div>

MY CSS

#changepic{
    background: rgb(158, 158, 158);
    padding: 5px;
    width: 140px;
    margin: 0 auto;
    color: #fff;
    text-align: center;
}
.fileInput {
    cursor: pointer;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    z-index: 99;
    font-size: 50px;
    opacity: 0;
    -moz-opacity: 0;
    filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);
}

MY SCRIPT

<script>
    function readURL() {
        var $input = $(this);
        if (this.files && this.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $input.next('.blah').attr('src', e.target.result).show();
            }
            reader.readAsDataURL(this.files[0]);
        }
    }

    $(".imgInp").change(readURL);

</script>

1 Answer 1

1

Try like this

<input type="file" id="file1" style="display:none" onchange="readURL()" />

<img src="something.jpg" onclick="$('#file1').click()"/> <!-- jquery -->

 <img src="something.jpg" onclick="document.getElementById("file1").click()"/>  <!-- javascript-->
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.