How to remove attached elements using delete


username

I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using append to add the button in the second div. That's my problem. This code works if the delete button is already in a div. (Indicates that it will be deleted when clicked). However, the attached button does not delete itself. How to add additional button to delete itself after click?

<title>AutoSuggestion using  Ajax, Jquery and PHP</title>

<script src="js/jquery-1.11.1.min.js" ></script>
<script type="text/javascript">

$(document).ready(function(){


            $('#add').click(function(){
                var return_value = "appended button (click to delete me)";
                $('.seconddiv').append('<button class ="remove">'+return_value+'</button>'); 


            });
            $( '.remove' ).click(function() {
              $( this ).remove();

            });

});


</script>

<div class="firstdiv"><button id ="add" >add</button></div>
<div class="seconddiv"><button class ="remove" >click to delete me</button></div>

Matt Fitz

It's a rebinding issue

http://jsfiddle.net/87pt31La/

$(document).ready(function(){

    //intial call on load
    bindFunc()

    $('#add').click(function(){
        var return_value = "appended button (click to delete me)";
        $('.seconddiv').append('<button class ="remove">'+return_value+'</button>'); 
         bindFunc()
    });

    //any Js code that needs to be rebinded
    function bindFunc(){
        $( '.remove' ).click(function() {
              $( this ).remove();
        });
    }


});

When you add new html, it's not bound to JS, so you have to rebind it.

Related


How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

Remove elements attached to image borders

Mostafa 2000 I'm using OpenCV to detect pneumonia in a chest x-ray using image processing, so I need to remove the additional area of the image border to get only the lungs, can anyone help me code in python? This picture explains the picture I want after appl

How to delete ajax attached container using data-id

z0mbieKale I'm adding a hidden field with user selections so that they can be reinserted later. I would also add a normal div to show the user their choice. I am trying to create a delete function that will delete visible rows as well as hidden rows. Now, I ca

How to delete elements in a list using delete order?

Ding medicine I have a list like this lst = [1,3,5]and a main list like thislost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ] I want to remove elements in the second list based on the index in the first list. This means I have to remove 'cat', 'you'

Style elements attached using jquery

Astroguy Pasa I have div element attached to parent div. Depending on the screen size, the div element should resize, but my efforts using jquery .css() keep failing. Please help. Below is my code sample var len = data.length; //a number comes here var i; for(

How to remove elements using JavaScript?

Patoshi Patoshi I have the following JavaScript objects: x = document.getElementById("region-footer-first").getElementsByClassName("block-title")[0]; How to remove x from DOM? Filip Minx Visit the parent node and tell it to delete its children. x.parentNode.r

How to remove elements using JavaScript?

Patoshi Patoshi I have the following JavaScript objects: x = document.getElementById("region-footer-first").getElementsByClassName("block-title")[0]; How to remove x from DOM? Filip Minx Visit the parent node and tell it to delete its children. x.parentNode.r

How to remove a container that is attached to the body

sdvnksv I created a container and added this to the body: // Create container var container = "<div class='foo'> ... </div>"; // Prepend it to the body $("body").prepend(container); Now, I want to fade out the container after a few seconds: setTimeout(functi

How to remove a container that is attached to the body

sdvnksv I created a container and added this to the body: // Create container var container = "<div class='foo'> ... </div>"; // Prepend it to the body $("body").prepend(container); Now, I want to fade out the container after a few seconds: setTimeout(functi

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in csproj using PowerShell

Zahar I try to remove the element <ItemGroup>from the .csprojfile but my script does nothing. What am I doing wrong? script: [xml]$csproj = Get-Content -Path ".\PackageTest.csproj" $refs = $csproj.Project.ItemGroup | Where-Object {$_.Content.Include -like "Pk

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in an array using RethinkDB

Dazik I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to remove elements from a list using lodash?

Samantha JT Star: I have an object that looks like this: var obj = { "objectiveDetailId": 285, "objectiveId": 29, "number": 1, "text": "x", "subTopics": [{ "subTopicId": 1, "number": 1 }, { "subTopicId": 2,

How to slowly remove elements using jQuery?

Facial mask $target.remove()The element can be removed, but now I want the process to decrease as the animation progresses, how can I do that? Greg: $target.hide('slow'); or $target.hide('slow', function(){ $target.remove(); }); Run the animation, then remov

How to remove HTML elements using Javascript?

Beginner Coders: I am a newbie. Can someone tell me how to remove HTML elements using raw Javascript (not jQuery). index.html <html> <head> <script type="text/javascript" src="myscripts.js" > </script> <style> #dummy { min-width: 200px; min-height: 200p

How to remove slice elements using append?

UmutcanErtürk : I want to delete some struct elements from an array of pointers this is my structure type User struct{ userName string userId int } I am trying to remove similar items. allUser []*User ------------ ------------ ------------ func Remo

How to remove elements that are not on the DOM using jQuery?

Ricardo I want to use jQuery to handle cloned elements that are not on the DOM to do something similar to them .remove(). Say I have the following code: var div= $('<div> <div id="div1"></div> </div>'); div.remove('#div1'); console.log(div.html()); The result

How to slowly remove elements using jQuery?

Facial mask $target.remove()The element can be removed, but now I want the process to decrease as the animation progresses, how can I do that? Greg: $target.hide('slow'); or $target.hide('slow', function(){ $target.remove(); }); Run the animation, then remov

How to remove elements from a list using lodash?

Samantha JT Star: I have an object that looks like this: var obj = { "objectiveDetailId": 285, "objectiveId": 29, "number": 1, "text": "x", "subTopics": [{ "subTopicId": 1, "number": 1 }, { "subTopicId": 2,

How to remove HTML elements using Javascript?

Beginner Coders: I am a newbie. Can someone tell me how to remove HTML elements using raw Javascript (not jQuery). index.html <html> <head> <script type="text/javascript" src="myscripts.js" > </script> <style> #dummy { min-width: 200px; min-height: 200p

How to remove disabled elements using JQUERY?

Michael I use jQuery 1.8.3. Here is my html input textbox: <input type="text" name="textinput-hide" id="textinput1" placeholder="Text input" value="" disabled> Here how I try to remove disabled elements: $('#textinput1').removeProp('disabled'); But the abo

How to remove elements in iframe using JQuery?

Hans En I have added a link element to the (ivuFrm_page0ivu1) content of the iframe and want to remove it when the button is selected. The code I am using is here: function removeAddedElements(){ console.log("called removeAddedElements()"); $("#ivuFrm_page0iv