javascript - View multiple browsed images with unique ID -
after searching in stackoverflow, found code can browse multiple images , preview them one-by one. however, when inspected element in browser, id each image same.
so, hope can me how modify code each image browsed has own unique id.
and
here html form:
<input id="imginput" type="file" name="file[]" multiple style="display:none;"/> <input type="button" onclick="$('#imginput').click();" value="choose file" /> <output id="result" ></output> <div style="margin-top:150px;" id="uploadedcontent"></div>
here js code:
var ftype = new array(); $("#imginput").change(function () { readurl(this); }); function readurl(input) { var files = input.files; var output = document.getelementbyid("result"); var count = 0; var count1 = 0; (var = 0; < files.length; i++) { var file = files[i]; var picreader = new filereader(); var divid = 'div_' + i; var spanid = 'span_' + i; picreader.addeventlistener("load", function (event) { var picfile = event.target; var picnames = files[count].name; var mimetypes = picfile.result.split(','); var mimetype1 = mimetypes[0]; var mimetype = mimetype1.split(':')[1].split(';')[0]; count++; var div = document.createelement("div"); div.setattribute('id', 'div_' + count); div.setattribute('class', 'divclass'); if (mimetype.match('image')) { div.innerhtml = "<img id='img_" + count + "' class='thumbnail' src='" + picfile.result + "'" + "title='" + picnames + "' width='96' height='80' alt='item image' style='position:relative;padding:10px 10px 10px 10px;' data-valu='" + mimetype + "'/><span class='boxclose' style='cursor:pointer' id='span_" + count + "'>x</span>"; } output.insertbefore(div, null); }); picreader.readasdataurl(file); } }
it needs count used on div, added img tiny change see fiddle below
http://jsfiddle.net/ekzfz9ck/4/
var ftype = new array(), count1=0;
$("#imginput").change(function () { readurl(this); }); function readurl(input) { var files = input.files; var output = document.getelementbyid("result"); var count = 0; var count1 = 0; (var = 0; < files.length; i++) { var file = files[i]; var picreader = new filereader(); var divid = 'div_' + i; var spanid = 'span_' + i; picreader.addeventlistener("load", function (event) { var picfile = event.target; var picnames = files[count].name; var mimetypes = picfile.result.split(','); var mimetype1 = mimetypes[0]; var mimetype = mimetype1.split(':')[1].split(';')[0]; count++; count1++; var div = document.createelement("div"); div.setattribute('id', 'div_' + count); div.setattribute('class', 'divclass'); if (mimetype.match('image')) { // below add id using count used div; div.innerhtml = "<img id='img_" + count1 + "' class='thumbnail' src='" + picfile.result + "'" + "title='" + picnames + "' width='96' height='80' alt='item image' style='position:relative;padding:10px 10px 10px 10px;' data-valu='" + mimetype + "'/><span class='boxclose' style='cursor:pointer' id='span_" + count + "'>x</span>"; } output.insertbefore(div, null); }); picreader.readasdataurl(file); } }
Comments
Post a Comment