My html code
<div id="delete">Delete</div>
My javascript code
$(function(){
$("#delete").click(function(){
var decision=decide("Do you really want to delete?");
});
});
function decide(str)
{
$("delete").after(str+'<button onclick="yes()">Yes</button><button onclick="no()">No</button>');
}
function yes(){return 1;}
function no(){return 0;}
Currently my yes and no are returning 0/1 but i want a return 0/1 from decide function depending upon no/yes.
Your code is asynchronous. You won’t be able to assign it to your
decisionvariable this way.In other words: your
decidefunction returns before the user clicks on any button, and will always returnundefined.Whatever you want to do with that Boolean, you’ll have to do from within those
yes/nofunctions.Here’s a quick sample of how to provide a callback:
Here’s a demo: http://jsfiddle.net/x2Lf9/