Merry Christmas to everyone.
I have 4 section each contains a empty img tag. While uploading a image it has to fit in the corresponding section.
When i click "Add image in section 1" and upload image it has to fix in Image_1 div like wise all the four. But when i insert click function in my code its not working.
Whats my mistake here.
<div class="pre_img" >
<span>
<img class="prw_img" src=".jpg" alt="your image" />
</span>
</div>
<input id="file" type="file" name="files[]" onChange="readURL(this);" />
<div id="Image_1">
<button> AddImage to section 1</button>
<img id="img_1" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_2">
<button> AddImage to section 2</button>
<img id="img_2" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_3">
<button> AddImage to section 3</button>
<img id="img_3" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_4">
<button> AddImage to section 4</button>
<img id="img_4" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
Heres my script
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.prw_img,#img_1').attr('src', e.target.result).width(112).height(112);
$('#img_1').css('display','inline');
};
reader.readAsDataURL(input.files[0]);
}
}
I have made a JSBIN for easy understanding I havn't added a click and tried to add that function then the entire script is not working
Merry Christmas to everyone.
I have 4 section each contains a empty img tag. While uploading a image it has to fit in the corresponding section.
When i click "Add image in section 1" and upload image it has to fix in Image_1 div like wise all the four. But when i insert click function in my code its not working.
Whats my mistake here.
<div class="pre_img" >
<span>
<img class="prw_img" src="http://www.iconshock./img_jpg/REALVISTA/general/jpg/128/preview_icon.jpg" alt="your image" />
</span>
</div>
<input id="file" type="file" name="files[]" onChange="readURL(this);" />
<div id="Image_1">
<button> AddImage to section 1</button>
<img id="img_1" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_2">
<button> AddImage to section 2</button>
<img id="img_2" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_3">
<button> AddImage to section 3</button>
<img id="img_3" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_4">
<button> AddImage to section 4</button>
<img id="img_4" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
Heres my script
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.prw_img,#img_1').attr('src', e.target.result).width(112).height(112);
$('#img_1').css('display','inline');
};
reader.readAsDataURL(input.files[0]);
}
}
I have made a JSBIN for easy understanding I havn't added a click and tried to add that function then the entire script is not working
Share Improve this question edited Dec 25, 2012 at 6:33 Vivek Dragon asked Dec 25, 2012 at 6:04 Vivek DragonVivek Dragon 2,2485 gold badges28 silver badges48 bronze badges 4- I just checked and it is working for section1 only – Satya Commented Dec 25, 2012 at 6:09
- Ya i mentioned only #img_1 in my script.... I have to click the button next to it and upload the image for that section – Vivek Dragon Commented Dec 25, 2012 at 6:10
- Did you leave the form element out of the sample code above? Otherwise, you're missing a vital part of the markup... – Tieson T. Commented Dec 25, 2012 at 6:38
- Ya i just remove my form to simplify the code – Vivek Dragon Commented Dec 25, 2012 at 6:41
2 Answers
Reset to default 1Give the image sections a class, or include them in a container to make the event listeners easier to handle
HTML:
<div class="pre_img">
<span><img class="prw_img" src=
"http://www.iconshock./img_jpg/REALVISTA/general/jpg/128/preview_icon.jpg" alt=
"your image"></span>
</div>
<form>
<input id="file" type="file" name="files[]" onchange="readURL(this);">
</form>
<div id="Image_1" class="imageSection"><button>AddImage to section 1</button> <img id=
"img_1" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
"img_1"></div>
<div id="Image_2" class="imageSection"><button>AddImage to section 2</button> <img id=
"img_2" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
"img_2"></div>
<div id="Image_3" class="imageSection"><button>AddImage to section 3</button> <img id=
"img_3" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
"img_3"></div>
<div id="Image_4" class="imageSection"><button>AddImage to section 4</button> <img id=
"img_4" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
"img_4"></div>
Then use the following script to display the uploaded image to a class [in this case, the activeImage
class], and to bind listeners to the buttons which toggles the "active" container
Js:
$(".imageSection button").click(function() {
$(".imageSection img").removeClass("activeImage");
$(this).parent().find("img").addClass("activeImage");
});
$(".imageSection:eq(0) img").addClass("activeImage");
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.prw_img,.activeImage').attr('src', e.target.result).width(112).height(112);
$('.activeImage').css('display', 'inline');
};
reader.readAsDataURL(input.files[0]);
}
}
JsBin: http://jsbin./imonub/8/edit
Try with this: http://jsbin./imonub/7/edit
var id = '1'; // set default id for first img tag
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.prw_img').attr('src', e.target.result).width(112).height(112);
$('#img_' + id).attr('src', e.target.result).width(112).height(112);
$('#img_' + id).css('display', 'inline');
};
reader.readAsDataURL(input.files[0]);
}
}
$(document).ready(function() {
$('button').click(function() {
id = $(this).html().replace('AddImage to section', '').trim();
});
});