Avatar of Moti Mashiah
Moti Mashiah
Flag for Canada asked on

Jquery

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.
jQuery* divJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Greg Alexander

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.
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Moti Mashiah

ASKER
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 () {
Moti Mashiah

ASKER
HI Julian,
I will try your solution now.

Please, can you tell me if you have any better idea other then check id ?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Moti Mashiah

ASKER
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

ASKER
Thank you Julian.
Julian Hansen

You are welcome
Just one more thing. can you advise how can I delete by avoiding check the id.
Did you answer this one?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.