This can't be done with JavaScript. In order for JavaScript to be able to
display an Ok and Cancel button for the user to press, JavaScript cannot
also be waiting to return a value for your function as that would block the
browser UI, since JavaScript is single-threaded. You have to use callback
functions instead. Here's an example:

http://jqueryui.com/demos/dialog/#modal-confirmation

view source: http://jqueryui.com/demos/dialog/modal-confirmation.html

Notice that the function that creates the dialog returns right away. Then
JavaScript is able to handle the dialog being interacted with, and
eventually either button click. Rather than those callback functions
returning a value, they carry out what your function would do depending on
which button is clicked.

- Richard

On Mon, Dec 21, 2009 at 10:00 AM, Jojje <jojjsus_chr...@hotmail.com> wrote:

> Hi!
>
> I´m trying to make a dialog box , where you can press ok or cancel.
> How do i set the function so that when a user press ok it returns
> true? I'm new to javascript an jQuery so bare with me but this is what
> i got so far...
>
> function dialogBox(type,blockUi,heading,message) {
>        var result;
>        var signImg;
>        var buttons;
>        var ok = '<div><input type="button" id="okBtn" value="Ok" /></div>';
>        var cancel = '<div><input type="button" id="cancelBtn"
> value="Cancel" /></div>';
>
>    //warning dialogbox
>        if (type == 'warning' || type == '') {
>                signImg='_img/signs/warning.png';
>                buttons = ok + cancel;
>        }
>    //Info dialogbox
>        else if (type == 'info') {
>                signImg = '_img/signs/info.png';
>                buttons = ok;
>        }
>    //Fatal dialogbox
>        else {
>                signImg = '_img/signs/error.png';
>                buttons = ok;
>        }
>    //Create dialogbox
>        function dialogBoxCreation() {
>        $('<div id="dialogBox"><img src="'+signImg+'" alt="sign" /
> ><h3>' + heading + '</h3><p>'+message+'</p>'+buttons+'</div>')
>        .appendTo('body')
>        .center()
>        .hide()
>        .fadeIn();
>    }
>    dialogBoxCreation();
>    //BlockUi
>        if (blockUi=='yes') {
>                $('<div
> id="dialogBg"></div>').height($('body').height()).width
> ('100%').css({
>            'position': 'absolute',
>            'background': '#000',
>            'opacity': '0.6',
>            'z-index': '1'
>        }).prependTo('body').fadeIn();
>        }
>    $('#okBtn').click(function() {
>               $('#dialogBox,#dialogBg').fadeOut(function() {
>                   $('#dialogBox').remove();
>                   $('#dialogBg').remove();
>                });
>        });
>        $('#noBtn').click(function() {
>                $('#dialogBox,#dialogBg').fadeOut(function() {
>                     $('#dialogBox').remove();
>                     $('#dialogBg').remove();
>                 });
>        });
> }
>
> Thanks in advance
>
> George
>

Reply via email to