1

I need to a a trigger to a dynamically created element then use event deligation to pick up the event and remove the dynamic element.

<div class="row">
    <div class="col-md-8 clickable"></div>
    <div class="col-md-4 display-clicks">
        <ul class="clicks">
        </ul>
    </div>
</div>

so lets say we have the above html within the display-clicks div the ul is appended with a list of co-ordinates clicked in the clickable div and next to each a remove button. the clickable div is populated with dynamically created spans all of which have a unique Id which is just the concatenated co-ordinates of the click. I can remove the appended li containing the co-ordinates and button using:

 $('.display-clicks').on('click','button',function(){
        // and this gets me the id of the dynamic span to be removed
        // from the clickable div
        var id = $(this).data('span-id');

        // this wont work
        $(id+'_span').remove();

        // neither will this
        $(id+'_span').trigger('myEvent');

        $(this).parent().remove();
        console.log(id);
  })

myEvent looks like this

   $('.clickable').on('myEvent', 'span', function(event){
       $(this).remove();
    })

Any ideas?

4
  • 1
    $('clickable') => $('.clickable') Commented Apr 13, 2016 at 13:32
  • yeah sorry thats just a typo .clickable is in the real code Commented Apr 13, 2016 at 13:33
  • What would the dynamically appended html looks like? Commented Apr 13, 2016 at 13:37
  • its just a span with an id like <span id="439.124_span">x</span> Commented Apr 13, 2016 at 13:42

2 Answers 2

1

$(this).parent().remove(); would remove the this element too, since you are removing it's parent. Therefore id cannot be defined and you can't select your span. Put that line at the end of the block. Also your id's cannot start with a number - http://www.w3schools.com/tags/att_global_id.asp

Sign up to request clarification or add additional context in comments.

3 Comments

I can see it looks that way I actually cast id to a variable before removing the parent and i can console.log it after trying to remove the span and li I will edit the question code.
also don't use digits in the start of the id value, you can use them after the first character - which should be a letter
very strange my code seems to work fine live but not on my dev machine sorry
0

Try this:

   $('.clickable').on('myEvent', 'span', function(event){
       $(this).remove();
    })

and your function is like this:

  $('.display-clicks').on('click','ul',function(){
        $(this).parent().remove();
        // and this gets me the id of the dynamic span to be removed
        // from the clickable div
        var id = $(this).data('span-id');

        // this works now
        $("#" + id+'_span_just_remove').remove();

        // this works too
        $("#" + id +'_span_trigger').trigger('myEvent');
  })

Plunker: https://plnkr.co/edit/ocZv95B3wBifX5zK7ob4?p=preview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.