javascript - Remove the image and rearrange the ID's number automatically -


say have selected 4 images computer file-input-form, , 4 images previewed before being uploaded. each selected image have unique id: first image have id of 1, second image has id of 2, , on third , last image.

the problem is, if remove second image, arrangment of image's id be:


<img id="img_1"> <img id="img_3"> <img id="img_4">


while id image number 2 (<img id="img_2">) has been removed structure above.

what want is, if second image remove, third image change position of image where id of third image should be: (<img id="img_2">) , id of fourth image** should be: (<img id="img_3">)

here code removing image:

$('body').on('click','.boxclose','',function(e){    var spanid = $(this).attr('id');         var splitval = spanid.split('_');         $('#div_' + splitval[1]).remove(); }); 

here code previewing image:

         var ftype = new array(), count1 = 0;      $("#imginput").change(function () {         readurl(this);      });     function readurl(input) {         var files = input.files;         var output = document.getelementbyid("result");            (var = 0; < files.length; i++) {             var file = files[i];             var picreader = new filereader();             var divid = 'div_' + i;             var spanid = 'span_' + i;             var count = 0;             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')) {                     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_" + count1 + "'>x</span>";                  }                  output.insertbefore(div, null);              });              picreader.readasdataurl(file);         }     } 

let's see in fiddle, , try select 4 images computer, , remove second image. , inspect elements. arrangements of ids not structured anymore after 1 of image removed.

do have use id's ? try adding class each image, , ond end use

$('.<classname>').eq(<index of element/image>) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -