Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag 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.
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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 () {
HI Julian,
I will try your solution now.

Please, can you tell me if you have any better idea other then check id ?
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,
Thank you Julian.
You are welcome
Just one more thing. can you advise how can I delete by avoiding check the id.
Did you answer this one?