Office Web Add-In dynamically adding buttons

118 Views Asked by At

I've just started to lean about Web Add-ins for Microsoft Word. My Add-in is built in Visual Studio 2017 and is a C# app. All of the examples I've seen so far hook up event handlers to buttons in the Office.initialize function in the accompanying JS.

Office.initialize = function (reason) {
    $(document).ready(function () {
        app.initialize();

        // Add event handler to button click event for the 'get-translation' button.
        $('#MyButton').click(get-translation);
    });
};

When initially loaded, I don't know all of the buttons I want to display to the user as they will be dynamically created based upon user selections in various combo boxes. How can I hook my events for newly created buttons as Office.initialize is only run when the page is initially loaded.

1

There are 1 best solutions below

0
On

You don't have to wire the .click event in Office.initialize, it is just the logical place to do it when you have static buttons to wire up. You can just as easily wire up the .click when you add the buttons to the DOM.