javascript - Dynamic selector in .on() method -


i have form represents quiz creating page want dynamically add questions , answers question. want respond on click event on newly created buttons.

problem onclick event executed on first button exists beginning.

here sample of code. have form id myform, div id questioni encapsulates stuff regarding i-th question (also button class new_answer adding answers particular question), variable curr_q (current question creating).

$(function () {     $("#myform").on('click', "#question" + curr_q + " .new_answer", function () {         alert("#question" + curr_q + " .new_answer");         }); }); 

here fiddle - http://jsfiddle.net/sruzic/8yt9jcqh/ (it little bit overwhelming problem when click on existing create question newly created create answer button not responing while 'old' create answer alerts selector question1 .new_answer newly created create answer button?!

thanks in advance.

update code . check demo

        $(function () {             var selector = "#question" + curr_q + " .new_question";             $(selector).click(function () {                 curr_q++;                 alert(curr_q);                 total_q++;                 $('#myform').append(generatequestionsubform(total_q))                  $("#myform").on('click', "#question" + curr_q + " .new_answer", function () {             alert("#question" + curr_q + " .new_answer");                  });              });         }); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -