javascript - why doesn't my jquery click function work after changing element values? -


so i'm making small quiz app object oriented js using object.create cloning method. have ol, , function called showvals() populates lis. seems working fine. i'm having trouble is: li click function give attr of ".selected' class seems work intitially, after click proceed , qn2.showvals() called no longer giving lis class of selected when clicked.

the data qn2 there. looks normal, except click function no longer working (giving lis class).

$(document).ready(function(){ qn1.showvals(); qn1.setans(1); // calling question1 answer  $('li').click(function(){ $('li').removeattr("class"); $(this).attr({"class": "selected"}); });  $('.proceed').click(function(e){     e.preventdefault();     if ($('.selected').html() == qn1.ctans) {      if (confirm("you correct")){     qn2.showvals();     qn2.setans(3);     };     }; });  });  var qn1 = { title:"the mouth of sauron", qn: "how did 'the mouth of sauron' meet demise?", img: "images/mouth-sauron.gif", ans: ["shot in back", "beheaded", "drowned in swamp", "sacrificed"], setans: function(x) {     this.ctans =  this.ans[x]; //setting correct answer }, showvals: function(){      $('#slide-title').text(this.title);      $('.question-box > p').text(this.qn);      $('#obj-img').attr("src", this.img);      $('ol').html('<li>'+this.ans[0]+'</li>'+'<li>'+this.ans[1]+'</li>'+     '<li>'+this.ans[2]+'</li>'+'<li>'+this.ans[3]+'</li>')    } }  var qn2 = object.create(qn1); qn2.title = "golemn"; qn2.qn = "what dude's name?"; qn2.ans= ["golemn", "gimli", "goober", "poop"] qn2.img = "images/golemn.gif"; 

this because li elements dynamically added. should try using jquery on(), allows bind event handler parent element must exists in dom, , can specify child/descendant selector call event handler. child element may still non-existent @ time event binding. in such case, call on() like:

$('ol').on('click', 'li', function () {...}); 

where ol exists.

alternatively, bind click handler dynamically generated li elements after have added them dom. although think more processor-time consuming assume have quiz questions ask user.


Comments

Popular posts from this blog

ios - UITEXTFIELD InputView Uipicker not working in swift -

Hatching array of circles in AutoCAD using c# -