javascript - Removing an AppendTo element jQuery - Stack Overflow

I have a function that appends dynamic a and img tags to a specific div ID :$('<a class="f

I have a function that appends dynamic a and img tags to a specific div ID :

$('<a class="fap-single-track" href=""><img src=".jpg').appendTo("#status_01");

But I want the user to be able to update this link (elsewhere on the page).

How do I remove the appended data from #status_01 when I don't know exactly what the data is going to be (the url and image links are being gathered from a remote API).

The problem I'm having is that when the user selects a new URL to populate the appendTo data, the old one is also there, so I end up with multiple <a> & <img> sets.

I've tried a bunch of different ways but nothing seems to work.

If I :

$('#status_01').remove(); // before adding new data

The status_01 DIV is no longer available in the dom for the next function.

$('#status_01').detach();

Doesn't work either.

I've also tried and mixture of prepend and some other stuff but none of it seems to get rid of the appendTo data.

Is it possible?

Or maybe there is something other than appendTo that I can use instead to attach the data in the first place?

I have a function that appends dynamic a and img tags to a specific div ID :

$('<a class="fap-single-track" href="http://web."><img src="http://web./image.jpg').appendTo("#status_01");

But I want the user to be able to update this link (elsewhere on the page).

How do I remove the appended data from #status_01 when I don't know exactly what the data is going to be (the url and image links are being gathered from a remote API).

The problem I'm having is that when the user selects a new URL to populate the appendTo data, the old one is also there, so I end up with multiple <a> & <img> sets.

I've tried a bunch of different ways but nothing seems to work.

If I :

$('#status_01').remove(); // before adding new data

The status_01 DIV is no longer available in the dom for the next function.

$('#status_01').detach();

Doesn't work either.

I've also tried and mixture of prepend and some other stuff but none of it seems to get rid of the appendTo data.

Is it possible?

Or maybe there is something other than appendTo that I can use instead to attach the data in the first place?

Share Improve this question edited Feb 11, 2016 at 10:20 Mr Lister 46.6k15 gold badges113 silver badges155 bronze badges asked Feb 3, 2016 at 16:08 GrantGrant 1,3372 gold badges17 silver badges40 bronze badges 1
  • yes have tried $(this).closest('#status_01').find('a.fap-single-track').remove(); but that's not doing it either – Grant Commented Feb 3, 2016 at 16:11
Add a ment  | 

2 Answers 2

Reset to default 3

This maybe ?

$('#status_01').html("");

Or, if you want to only remove the last :

$('#status_01').children().last().remove();

In addition to @Remy's correct answer, you can also use .empty():

$('#status_01').empty();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744739659a4590952.html

相关推荐

  • javascript - Removing an AppendTo element jQuery - Stack Overflow

    I have a function that appends dynamic a and img tags to a specific div ID :$('<a class="f

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信