2

I made JS for displaying an image after selecting it. Here is the code

html code

<input type="file" id="file_photo" onchange="preview_image(event)" name="item_image"  ></input>
<img id='output_image'/>

JS code

function preview_image(event){

    document.getElementById('output_image').style.display='block';
    var reader = new FileReader();
    reader.onload = function(){
        var output = document.getElementById('output_image');
        output.src = reader.result;
    }
    reader.readAsDataURL(event.target.files[0]);
};

It success to display. But the question is, could I put the output image inside css url background image?

If the html is like this.

<label for="file_photo" class="file">  
   <div class="camera_icon" style="background: url() center center no-repeat #d7d7d7;background-size: cover;">
         <img src="/public/img/stock/icon07.png">
         <img src="/public/img/stock/icon06.png" class="plus_icon">
    </div>
    <input type="file" id="file_photo" onchange="preview_image(event)" name="item_image"  ></input>
 </label>

could I put the output image in

style="background: url() center center no-repeat #d7d7d7;background-size: cover;"

because I need to follow the css file.

1

3 Answers 3

1

Here is a working fiddle for you mate : http://jsfiddle.net/qvxg6ok4/12/

You need to assign a formulated URl property for div's background image with the data url you are getting from the file control.

output.style.backgroundImage = "url('" + reader.result + "')"

This fiddle is with searching with class name : http://jsfiddle.net/qvxg6ok4/14/

Sign up to request clarification or add additional context in comments.

Comments

1

You need to have the path/src for the image you want to apply.

and then you may user the following code

document.getElementById('camera_icon').style.backgroundImage="url({{images/img.jpg}})";

Try using this fiddle http://jsfiddle.net/LvsYc/15724/

    <label for="file_photo" class="file">  
   <div class="camera_icon" id="camera_icon">
         <img src="/public/img/stock/icon07.png">
         <img src="/public/img/stock/icon06.png" class="plus_icon">
    </div>
    <input type="file" id="file_photo" name="item_image"  ></input>
 </label>
    <style>
    .camera_icon {
  background-repeat: no-repeat;
  background-size: cover;
  background-color:#d7d7d7;
  background-position: center center;
  height:300px;
}
    </style>
    <script>
    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                //$('#blah').attr('src', e.target.result);
                document.getElementById('camera_icon').style.backgroundImage="url( " + e.target.result + ")";
            }

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

    $("#file_photo").change(function(){
        readURL(this);
    });
    </script>

1 Comment

thank you guys for helping me.. i really appreciate it so much!!
0

have you tried like this,

function preview_image(event){

document.getElementsByClassName('camera_icon').style.display='block';
var reader = new FileReader();
reader.onload = function(){
    var output = document.getElementsByClassName('camera_icon');
    output.style.backgroundImage= reader.result;
}
reader.readAsDataURL(event.target.files[0]);
};

use background image instead of src of image

2 Comments

Are you not supposed to get array of objects matching "getElementsByClassName" ?
And the data url is not going to work with simple assignment to backgroundimage. And on top of that, there is no such property for an object

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.