I am currently working on implementing a new upload feature for users of our forum. This feature will allow them to upload a personal picture. Here is what I have developed so far:
HTML
<label>Profile image</label>
<div class="photo" id="photoPreview"></div>
<input type="file" value="forumPhoto" onchange="return changePhoto(this);">
JAVASCRIPT
function showPhotoPreview() {
var photoUrl = $.trim($("#photo").val());
var img = $("#photoPreview img");
if (photoUrl != "") {
if (img.length == 0) {
img = $("<img />").appendTo($("#photoPreview"));
}
img.prop("src", photoUrl);
}
else {
img.remove();
}
}
function changePhoto(sender) {
var value = $(sender).val();
sender.selectedIndex = 0;
switch (value) {
case "upload":
assignPicture();
break;
}
showPhotoPreview();
return false;
}
function assignPicture() {
var forumPhoto = $("#forumPhoto").val();
if (forumPhoto == "") {
alert("You must specify an address to use forumPhoto.");
return;
}
$("#photo").val(forumPhoto);
}
The current challenge I am facing is that the preview picture is not displaying properly.
JsFiddle: http://jsfiddle.net/qLsY7/