Returning a Value from a jQuery Dialog Button Instead of Running a Function

Posted by kamera on Stack Overflow See other posts from Stack Overflow or by kamera
Published on 2014-06-06T15:08:02Z Indexed on 2014/06/06 15:25 UTC
Read the original article Hit count: 223

Filed under:
|

I'm working on an admin UI and I can't wrap my head around how to get a specific kind of modal behaviour. I'm using jQuery.

I already have a system for modal dialogs in place, where an openModal(settings) function opens a modal. Based on the settings, it will show/hide or sometimes inject various dialogs.

I also have a generic confirmation dialog and this is the one I'm not sure how to make work. The layout is simple:

<div id="confirmation-dialog">
    <h2>Are you sure?</h2>
    <button class="button.yes">Yes</button>
    <button class="button.no">No</button>
</div>

I could just hard-code each button to do what I need it to do but I would like this to be more generic. Ideally, I would like it to function like this: (I know this doesn't work)

function deleteImportantThing() {
    if (confirmThis() == true) {
        // Delete the thing
    } else {
        // Do nothing, close dialog
    }
}

function confirmThis() {
    openModal({kind: "confirm"});
    // Check which button is clicked, then return true or false
}

I guess I just don't know enough JavaScript to get this to work, or to even figure out where to look. Any advice?

I know there are modal dialog plug-ins out there but I'd have to change how the rest of my modals work to integrate them, and I'd like to do this myself if at all possible.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery