0
jQuery(function ($) {
$('.confirmbttn').click(function (e) {
e.preventDefault();


    // example of calling the confirm function
    // you must use a callback function to perform the "yes" action
    confirm("", function () {

    });
});
});

function confirm(message, callback) {
$('.confirm').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});

}


jQuery(function ($) {
$('.confirmbttn2').click(function (e) {
e.preventDefault();


    // example of calling the confirm function
    // you must use a callback function to perform the "yes" action
    confirm("", function () {

    });
});
});

function confirm(message, callback) {
$('.confirm').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});

}

UPDATE: So my code now works fine because I changed the name of my functions. Thanks but now I would like to only use "jQuery(function ($) {..." once. I'm using it twice. How can I combine all of my functions using only one "jQuery(function ($) {..."

jQuery(function ($) {
    $('.confirmbttn').click(function (e) {
        e.preventDefault();
        execChart1("", function () {
        });
    });
});

function execChart1(message, callback) {
    $('.confirm').modal({
        closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
        position: ["20%",],
        overlayId: 'confirm-overlay',
        containerId: 'confirm-container', 
        onShow: function (dialog) {
            var modal = this;

            $('.message', dialog.data[0]).append(message);

            // if the user clicks "yes"
           $('.yes', dialog.data[0]).click(function () {
                // call the callback
                if ($.isFunction(callback)) {
                    callback.apply();
                }
               // close the dialog
                modal.close(); // or $.modal.close();
            });
        }
    });
}


jQuery(function ($) {
$('.confirmbttn1').click(function (e) {
    e.preventDefault();
    execChart2("", function () {
    });
});
});

function execChart2(message, callback) {
$('.confirm1').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});
}
8
  • 1
    delete one instance of function confirm () {. there can only be one, the second overrides the first. Commented Oct 2, 2013 at 21:33
  • 1
    You probably shouldn't call your function confirm(), since there's a standard window.confirm(). Commented Oct 2, 2013 at 21:35
  • 1
    @KevinB They're in different scopes. Commented Oct 2, 2013 at 21:38
  • Still, it makes little sense to reuse the name Commented Oct 2, 2013 at 21:39
  • 1
    no... i took his code, hit tidy up. That's it. The function declaration is outside of the jQuery(function(){...}); i didn't attempt to make the code actually work because that's irrelevant to my point. Commented Oct 2, 2013 at 21:44

1 Answer 1

1

You need to bind a local variable to the instance that was clicked on:

$('.confirmbttn, .confirmbttn2').click(function (e) {
    e.preventDefault();
    var theButton = this;

    // example of calling the confirm function
    // you must use a callback function to perform the "yes" action
    confirm("", function () {
        // Do stuff with theButton
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.