1

I've got an mvc app in .net that i'm trying to dynamically add buttons. Below is a sample of the jquery i'm using. It's a fairly long sequence of html that's appended but it works and those buttons are added. But when the buttons click they don't seem to activate a click event handler that i've got.

$('#imageCont').append('<div class="row tier" id="tier'+ tierIdCount +'"  ><hr/><h3>Tier</h3><button type="button" class="btn btn-default addPerson">Add Person</button><button type="button" class="btn btn-default removePerson">Remove Person</button></div>');

Thanks.

0

1 Answer 1

2

I imagine you have event handler with jQuery like this:

$('.removePerson').on('click', 
   function(e){
       //do some stuff
   });

And if you changed this first line with

$(document).on('click', '.removePerson',
   function(e){
       //do some stuff
   });

everything should start working as you expect. Dynamically added html elements have to use delegated events, because listeners are attached to elements which are already present in DOM

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

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.