-1

I am working on a project and there I am getting a value from input tag and then insert input value in a div and appending on screen with children element.

And What I want that when user click on children element then parent div would be removed and for this I'm using a function. That is working when I am using by default a section but when I append a section and then click on that's children where a function call like when user click on it's children parent section would be removed but my functionality not working.

$('#btn').click(function() {
  var menuFieldName = $('#text').val();
  $('.div').append('<div class="a">' + menuFieldName + '<span>X</span></div>');
  $('#text').val('');
});

$('.div .a span').on('click', function() {
  $(this).parent().remove();
});
.a {
  display: inline-block;
  padding: 5px 35px 5px 10px;
  position: relative;
  background: #eee;
  border-radius: 20px;
  margin: 10px;
}

.a span {
  position: absolute;
  top: 0;
  right: 0;
  background: #333;
  color: #fff;
  height: 28px;
  width: 28px;
  text-align: center;
  line-height: 28px;
  border-radius: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
  <div class="a">Test <span>X</span></div>
</div>

<input type="text" id="text">
<button id="btn">Add</button>

https://jsfiddle.net/jafaruddeen/rag71ma0/

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75

3 Answers3

2

You are appending elements dynamically but you are not attaching any event handler to the newly added elements. To solve this you can use event delegation, you can attach events to .div like $('.div').on('click', '.a span', function() {

$('#btn').click(function() {
  var menuFieldName = $('#text').val();
  $('.div').append('<div class="a">' + menuFieldName + '<span>X</span></div>');
  $('#text').val('');
});

$('.div').on('click', '.a span', function() {
  $(this).parent().remove();
});
.a {
  display: inline-block;
  padding: 5px 35px 5px 10px;
  position: relative;
  background: #eee;
  border-radius: 20px;
  margin: 10px;
}

.a span {
  position: absolute;
  top: 0;
  right: 0;
  background: #333;
  color: #fff;
  height: 28px;
  width: 28px;
  text-align: center;
  line-height: 28px;
  border-radius: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="div">
  <div class="a">Test <span>X</span></div>
</div>

<input type="text" id="text">
<button id="btn">Add</button>
Dij
  • 9,761
  • 4
  • 18
  • 35
0

The reason you can't remove it is because when you register the event, the element on which you try to register it doesn't exist yet.

You need to use event delegation

$('body').on('click', '#my-element', function(){...});
Radioreve
  • 3,173
  • 3
  • 19
  • 32
0

you have to call click function after append.. or call click function globally(body element) and match the span..

$(document).ready(function(){
        $('#btn').on('click',function(){
            var menuFieldName = $('#text').val();
            $('.div').append('<div class="a">'+menuFieldName+'<span class="close">X</span></div>');
            $('.div .a span').on('click', a);
            $('#text').val('');
        });

            $('.div .a span').on('click', a);    
    function a() {
       $(this).parent().remove();
    }
});

check your js fiddle. fixed it -> https://jsfiddle.net/jafaruddeen/rag71ma0/

Rajapandian
  • 216
  • 2
  • 7