1

This is where I dynamically create an image:

function handleFileSelect(evt) {
                var files = evt.target.files; // FileList object
                // Loop through the FileList and render image files as thumbnails.
                for (var i = 0, f; f = files[i]; i++) {
                    // Only process image files.
                    if (!f.type.match('image.*')) {
                        continue;
                    }
                    fileNames.push(f.name);
                    var reader = new FileReader();
                    // Closure to capture the file information.
                    reader.onload = (function(theFile) {
                        return function(e) {
                            // Render thumbnail.
                            var span = document.createElement('span');
                            span.innerHTML = ['<img class="thumb" id="',escape(theFile.name),'" src="',e.target.result,
                            '" title="', escape(theFile.name), '"/><input class="delete" type="button" value="delete" name="',escape(theFile.name),'"/>'].join('');
                            document.getElementById('list').insertBefore(span, null);
                        };
                    })(f);
                    // Read in the image file as a data URL.
                    reader.readAsDataURL(f);
                }

And this is where I want to delete the image when I press the button:

$('#list').on("click",".delete",function(e){
                $(e.target).remove();

            });

1 Answer 1

2

as image is before the button you are clicking

$('#list').on("click",".delete",function(e){
   $(this).prev('img').remove();
});
Sign up to request clarification or add additional context in comments.

2 Comments

can also reference on the <img> id to execute the remove() function.
As OP is using .delete to attach event handler - The image can be referenced with $(this) . alternatively if he wan't to remove with id as you said You need to get the ID of the image first $(this).prev('img').attr('id');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.