How can I remove a dynamic data-callback from a button?

Hello all,

I have some code I did not create but what is essentially is there is a dynamic window that gets created and button id's are being passed to this javascript function.   In that function the buttons are being dynamically created for the window such as below.  I need to remove it as what happens is the function gets called more than once the click events get fired multiple times.   Any idea how to delete this?  I try removing the button such as:

 $("#" + btnId).remove();

Before creating it again as it's going into the DOM but this seems to not fix the issue.  Here is the code and essentially I need to kill the buttons first.

   lms.ModalButtonDialogWithRemoteContent = function (obj) {

        if (LMS.IsUndefinedOrNull(obj) || LMS.IsUndefinedOrNull(obj.dialogId) || LMS.IsUndefinedOrNull(obj.buttons) || LMS.IsUndefinedOrNull(obj.content)) {
            console.log("One or more critical properties was not defined, exiting...");
            return null;
        }

        var buttonsHtml = '<div class="buttonDialogWrapper">REPLACE</div>';
        var buttonBuffer = '';

        for (key in obj.buttons) {
            var attr = 'data-callback="' + key + '"';
            var btnId = "kWinBtn" + key;

            $("#" + btnId).remove();

            buttonBuffer += ['<button id="' + btnId + '" class="k-button"', ' ', 'value="', key, '" ', attr, '">', key, '</button>'].join('');

            var selector = '[' + attr + ']';

            var func = obj.buttons[key];

            if (LMS.IsUndefinedOrNull(func) || typeof func !== "function") {
                func = function () {
                    return;
                };
            }

            $("body").on("click", selector, func);
        }

        buttonsHtml = buttonsHtml.replace(/\bREPLACE\b/, buttonBuffer);

        var template = '<div><div style="text-align:center">CONTENT</div><div style="text-align:center">BUTTONS</div></div>';
        var body = template;

        body = body.replace(/\bCONTENT\b/, obj.content);
        body = body.replace(/\bBUTTONS\b/, buttonsHtml);

        var dialogPlaceholderSelector = "#" + obj.dialogId;
        var dialogBody = $(dialogPlaceholderSelector);


        if (dialogBody.length == 0) {
            var htmlStr = '<div id="' + obj.dialogId + '"></div>';
            jqry("body").append(htmlStr);
        }

        jqry(dialogPlaceholderSelector).html(body);

        $(dialogPlaceholderSelector).kendoWindow({
            actions: ["Close"],
            draggable: true,
            height: obj.h || 'auto',
            width: obj.w || 'auto',
            modal: true,
            pinned: true,
            resizable: true,
            title: obj.title || "Message",
            visible: false,
            deactivate: function () { this.destroy(); },
            refresh: function () { this.center(); }
        });

        return $(dialogPlaceholderSelector).data("kendoWindow");

    };

Open in new window

sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
Try

 $("body").off("click").on("click", selector, func);

But I suggest you narrow your selection to the nearest container and not the whole body
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.