Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    141
    Thanks
    3
    Thanked 20 Times in 20 Posts

    Jquery Append and then remove

    I am using the append method to add html to the page. This works perfectly, however when appending that html I also need to append a remove button. It will append the button however it does not work, it wont remove the element.

    If I hard code the remove button in it works, but when appeneded nothing happens.

    I have a JSfiddle,

    When you click on 'Refund Item' the words 'Hello' followed by 'Take it' appear. The take it when clicked should remove the 'Hello' element, but it does not. Ny ideas??
    Last edited by itxtme; 02-27-2013 at 01:09 AM. Reason: add fiddle

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,932
    Thanks
    56
    Thanked 552 Times in 549 Posts
    adding a simple click listener to an element will only work if the element exists at the time that the code is run. To add a listener to an element that doesn't exist yet, one solution is to delegate the event:

    Code:
     $("#there").on("click"," #remove_1", function () {
    	 $('.take').remove();
     });

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    141
    Thanks
    3
    Thanked 20 Times in 20 Posts
    fantastic, thanks for that!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •