Jquery

Moti Mashiah
Moti Mashiah used Ask the Experts™
on
Hi Guys,

I have some issue with removing images after I add them through jquery.
Here is my example:
I have this div:
<div id="imgshow1" class="col-sm-3 col-md-3 form-group">
                                            <div class="img-wrapi">
                                                <span class="close">&times;</span>
                                                <img src="~/pics/no_image.gif" width="150" height="150" id="pic1" />
                                            </div>
                                        </div>

Open in new window

Here is my jQuery..see how I add image:
$("#imgshow1").html('<div class="img-wrapi"><span class="close">&times;</span><img src="/pics/' + data + '" width="150" height="150"/></div>')

Open in new window


and here is my code for delete image which is not working:
 $('.img-wrapi .close').on('click', function () {
        debugger;
        var id = $(this).closest('.img-wrapi').find('img').attr('id');
 if (id == "pic1") {
            var test = $("#pic1").remove();
        }

Open in new window


The issue I have here is when I delete the image at the div and then add new one I can't delete the image again.
I can only delete the images I didn't create through jquery.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
The problem is that your function is only going to remove images with the id of pic1. When you add the image, you are not giving it the pic1 id.

$("#imgshow1").html('<div class="img-wrapi"><span class="close">&times;</span><img src="/pics/' + data + '" width="150" height="150" id="pic1" /></div>')

Open in new window


The above would now delete the created picture through your function.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
... except that is going to result in non-unique id's which is not a good idea.

This should do it

$('body').on('click', '.img-wrapi .close', function () {
  $(this).parent().remove();
});

Open in new window

The parent of the close button is the container so just remove at the parent level
Moti Mashiah.NET Developer

Author

Commented:
Yep, I'm aware about that one.

The issue is that when I delete the image then add image the function even doesn't run

like this one:
 $('.img-wrapi .close').on('click', function () {
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Moti Mashiah.NET Developer

Author

Commented:
HI Julian,
I will try your solution now.

Please, can you tell me if you have any better idea other then check id ?
Moti Mashiah.NET Developer

Author

Commented:
Julian, your solution is working thank you soo much.

Just one more thing. can you advise how can I delete by avoiding check the id.

thanks,
Moti Mashiah.NET Developer

Author

Commented:
Thank you Julian.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome
Just one more thing. can you advise how can I delete by avoiding check the id.
Did you answer this one?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial