I’m looking for a way to have a div background image change when the user chooses an image from a form input. As you can see below I have a input type of file. Once the user chooses the file they want I would like the div#imgHolder to change the background based on the file chosen. Any thoughts?
<div id="simpleDialog" style="width: 350px; height: 350px; display:none;" scrolltop="0">
<br>
<div id="imgHolder" style="width:200px;height:200px;background-image: url(images/gray.png)"></div>
<br>
<form id="userForm" align="center">
<fieldset>
<legend>Artist Info</legend>
<input type="file" name="artistImage" id="artistImage" style="border: none;float:left"><br><br>
<label for="text" style="float:left">Enter URL:</label>
<input type="text" name="website" id="website" value="" required="required">
</fieldset>
</form>
<input type="button" id="submit" value="Submit" />
<input type="button" id="cancel" value="Cancel" />
</div>
Since you tagged this html5, you understand that this is an advanced browser-feature.
What you’re asking for will require the FileReader, available in most all modern browsers (even IE).
I just tested the above code in Internet Explorer 10, Chrome 19/21, Firefox 12/13, and Opera 11.64 and found that it worked with no problems.
Older browsers, which don’t support the FileReader, will just remain silent. No images shown, no exceptions raised.
Fiddle: http://jsfiddle.net/jonathansampson/K3A9r/