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.
LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg AlexanderLead DeveloperCommented:
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.
0
Julian HansenCommented:
... 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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Moti Mashiah.NET DeveloperAuthor 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 () {
0
Determine the Perfect Price for Your IT Services

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

Moti Mashiah.NET DeveloperAuthor Commented:
HI Julian,
I will try your solution now.

Please, can you tell me if you have any better idea other then check id ?
0
Moti Mashiah.NET DeveloperAuthor 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,
0
Moti Mashiah.NET DeveloperAuthor Commented:
Thank you Julian.
0
Julian HansenCommented:
You are welcome
Just one more thing. can you advise how can I delete by avoiding check the id.
Did you answer this one?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.