Using a callback is a great way to perform an action after an event has occured. In this case the event would be 'the user filling in a response'. Check out a working example here http://jsfiddle.net/Q2qUK/2/.
<div id="dialog-text"></div>
<input id="dialog-input" />
In the cPrompt function, you can run the callback function just like any other function when the time is right. Instead of returning a value you pass the results as a parameter to the callback function.
function cPrompt(mssg, inputFieldId, messageFieldId, callback){
var message = mssg;
var inputField = $('#'+inputFieldId);
var userInput = "";
cNotify(messageFieldId, message);
// Detect enter space being pressed in inputField
inputField.on('keyup', function (e) {
if (e.keyCode == 13) {
userInput = inputField.val();
callback(userInput);
// If you want the callback to only be executed once,
// unbind the keyup event afterwards.
inputField.off('keyup');
// Empty the input field after processing the user's message.
inputField.val('');
}
});
}
As an example of how to let your coding respond to the user input, I've created this cNotify function to show the user input in the dialog-text element.
function cNotify(messageFieldId, message){
$('#' + messageFieldId).append('<div>' + message + '</div>');
}
To pass the callback use an anonymous function as a parameter to the cPrompt function.
cPrompt('What is your class?', 'dialog-input', 'dialog-text', function(_cprompt){
// Here you put all the code you want to run after the user pressed enter.
cNotify('dialog-text', _cprompt);
});