Waiting for user input with jQuery and Javascript -


i trying wait take user's input (clicking button) before continuing program. simplified version of code this:

$(document).ready(function(){     var answer = $(".question").on("click", "button", takeanswer);     alert(answer); }); //code set next question once user has answered previous question  function takeanswer(){     var answer = ($(".question button").data("answer"));     alert(answer);     return answer; } 

the html body contains this:

<p class="question"><button data-answer="1">push me</button></p> 

on loading, html alerts [object object]. once click button, page alerts 1 supposed to. think problem return line executing without awaiting user's click. research suggests need use callback function (there several discussions on waiting user input), i'm confused because thought putting takeanswer code in separate function keep subsequent code executing.

for further context, goal ask series of choose-your-own-adventure style questions, changing later questions based on earlier user input. intend use jquery remove previous question , place new question, according logic within javascript file. edit 1: should mention pretty new , trying teach myself (if that's not obvious).

edit 3: here full context in jsfiddle. http://jsfiddle.net/t7vhc/ problem "undefined" error arising line 144 of js.

edit 2:

eventually want add other buttons , capture button clicked. here simplified example of (dysfunctional because, think, there no callback) attempt implement i'm doing:

function takeanswer(){   $(document).ready(function(){     $(".question").on("click", "button", function(){       return $(this).data("answer");     });   }); }  alert(takeanswer()); 

html:

<body>   <p class="question"><button data-answer="1">push me</button></p>   <p class="question"><button data-answer="2">or push me</button></p> </body> 

thanks.

for complete code, use:

$(document).ready(function(){   $(".question").on("click", "button", takeanswer); }); //code set next question once user has answered previous question  $(takeanswer);  function takeanswer(){   var answer = $(".question button").data("answer");   alert(answer); } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -