We help IT Professionals succeed at work.

Jquery

Moti Mashiah
Moti Mashiah asked
on
130 Views
Last Modified: 2017-04-06
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

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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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 () {
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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions