Delete button for each image.

remove button in each thumbnail

Hi I need your help for my codes. I want to preview multiple images before uploading and each image has a delete button. But my code doesn't work when I use a div setting in each delete button.

first, my codes are like THIS FIDDLE 1 . and when i add some changes do THIS FIDDLE 2

my HTML:

<body>
<header>
    <h1>File API - FileReader</h1>
</header>
<article>
    <label for="files">Select multiple files: </label>
    <input id="files" type="file" multiple/>
    <output id="result" />
</article>

      

and CSS:

body{
font-family: 'Segoe UI';
font-size: 12pt;
}

header h1{
font-size:12pt;
color: #fff;
background-color: #1BA1E2;
padding: 20px;

 }
 article
{
width: 80%;
margin:auto;
margin-top:10px;
}


 .thumbnail{

height: 100px;
margin: 10px;    
}

      

these are my javascripts:

 window.onload = function(){

//Check File API support
if(window.File && window.FileList && window.FileReader)
{
    var filesInput = document.getElementById("files");

    filesInput.addEventListener("change", function(event){

        var files = event.target.files; //FileList object
        var output = document.getElementById("result");

        for(var i = 0; i< files.length; i++)
        {
            var file = files[i];

            //Only pics
            if(!file.type.match('image'))
              continue;

            var picReader = new FileReader();

            picReader.addEventListener("load",function(event){

                var picFile = event.target;

                var div = document.createElement("div");

                div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                        "title='" + picFile.name + "'/> <a href='#' class='remove_pict'>X</a>";

                output.insertBefore(div,null);            

            });

             //Read the image
            picReader.readAsDataURL(file);
        }                               

    });
}
else
{
    console.log("Your browser does not support File API");
}
}

      

thanks for the promotion. any suggestions are greatly appreciated ^^

+3


source to share


2 answers


Image and anchor removal are children of the object div

. Put an event click

on each a

, then remove the parent. Therefore, when the user clicks on the x icon , the selected image will be deleted.

div.children[1].addEventListener("click", function(event){
    div.parentNode.removeChild(div);
});

      



see demo at http://jsfiddle.net/L45LW/5/

+3


source


$("#result").on( "click",".remove_pict",function(){
    $(this).parent().remove();
});

      



This might help you

0


source







All Articles