How to simplify multiple functions that have different ids



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to simplify the following code:



var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);
document.getElementById("newFloorplan").addEventListener("click", function (event)
filesystem.newFloorplan();
event.preventDefault();
);
document.getElementById("showOpenWindow").addEventListener("click", function (event)
filesystem.showOpenWindow();
event.preventDefault();
);
document.getElementById("saveFloorplan").addEventListener("click", function (event)
filesystem.saveFloorplan();
event.preventDefault();
);
document.getElementById("saveFloorplanAs").addEventListener("click", function (event)
filesystem.saveFloorplanAs();
event.preventDefault();
);
document.getElementById("showRemoveWindow").addEventListener("click", function (event)
filesystem.showRemoveWindow();
event.preventDefault();
);


It's starting to take up a lot of room and look messy.










share|improve this question




























    1















    I am trying to simplify the following code:



    var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);
    document.getElementById("newFloorplan").addEventListener("click", function (event)
    filesystem.newFloorplan();
    event.preventDefault();
    );
    document.getElementById("showOpenWindow").addEventListener("click", function (event)
    filesystem.showOpenWindow();
    event.preventDefault();
    );
    document.getElementById("saveFloorplan").addEventListener("click", function (event)
    filesystem.saveFloorplan();
    event.preventDefault();
    );
    document.getElementById("saveFloorplanAs").addEventListener("click", function (event)
    filesystem.saveFloorplanAs();
    event.preventDefault();
    );
    document.getElementById("showRemoveWindow").addEventListener("click", function (event)
    filesystem.showRemoveWindow();
    event.preventDefault();
    );


    It's starting to take up a lot of room and look messy.










    share|improve this question
























      1












      1








      1








      I am trying to simplify the following code:



      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);
      document.getElementById("newFloorplan").addEventListener("click", function (event)
      filesystem.newFloorplan();
      event.preventDefault();
      );
      document.getElementById("showOpenWindow").addEventListener("click", function (event)
      filesystem.showOpenWindow();
      event.preventDefault();
      );
      document.getElementById("saveFloorplan").addEventListener("click", function (event)
      filesystem.saveFloorplan();
      event.preventDefault();
      );
      document.getElementById("saveFloorplanAs").addEventListener("click", function (event)
      filesystem.saveFloorplanAs();
      event.preventDefault();
      );
      document.getElementById("showRemoveWindow").addEventListener("click", function (event)
      filesystem.showRemoveWindow();
      event.preventDefault();
      );


      It's starting to take up a lot of room and look messy.










      share|improve this question














      I am trying to simplify the following code:



      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);
      document.getElementById("newFloorplan").addEventListener("click", function (event)
      filesystem.newFloorplan();
      event.preventDefault();
      );
      document.getElementById("showOpenWindow").addEventListener("click", function (event)
      filesystem.showOpenWindow();
      event.preventDefault();
      );
      document.getElementById("saveFloorplan").addEventListener("click", function (event)
      filesystem.saveFloorplan();
      event.preventDefault();
      );
      document.getElementById("saveFloorplanAs").addEventListener("click", function (event)
      filesystem.saveFloorplanAs();
      event.preventDefault();
      );
      document.getElementById("showRemoveWindow").addEventListener("click", function (event)
      filesystem.showRemoveWindow();
      event.preventDefault();
      );


      It's starting to take up a lot of room and look messy.







      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 14:50









      Jon NowakJon Nowak

      54110




      54110






















          5 Answers
          5






          active

          oldest

          votes


















          2














          Simply call wrapper function with dynamic values for ID and function name:



          var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

          function bindEvent(id, callbackFunction)
          document.getElementById(id).addEventListener('click', function (event))
          event.preventDefault();

          filesystem[callbackFunction]();



          bindEvent('newFloorplan', 'newFloorplan');
          bindEvent('showOpenWindow', 'showOpenWindow');
          ...





          share|improve this answer























          • I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

            – Grzesiek Danowski
            Nov 15 '18 at 15:03











          • @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

            – Alnitak
            Nov 15 '18 at 15:10











          • Awesome, Thank You-

            – Jon Nowak
            Nov 15 '18 at 15:35











          • Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

            – Jon Nowak
            Nov 15 '18 at 15:35



















          1














          You can access the property of an object by a variable containing a string using bracket notation.






          var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

          ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
          var el = document.getElementById(id);
          el.addEventListener('click', function(event)
          filesystem[id]();
          event.preventDefault();
          );
          );








          share|improve this answer






























            0














            const filesystem = new fp.FloorplanFilesystem (myFloorplan, fp.FILESYSTEM_UI_STATE)

            const ids = ['newFloorplan', 'showOpenWindow', 'saveFloorplan', 'saveFloorplanAs', 'showRemoveWindow']
            const handler = id => event => (filesystem [id] (), event.preventDefault ())

            ids.forEach (id => document.getElementById (id).addEventListener ('click', handler (id)))





            share|improve this answer






























              0














              Since your elements use the name of the function that you want to invoke as their ID, you can extract that within the event handler:



              var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

              function fs_handler(event)
              filesystem[event.target.id]();
              event.preventDefault();


              ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
              document.getElementById(id).addEventHandler('click', fs_handler);
              );


              This should be generally preferred over creating a new callback function object for each item.



              Furthermore, if the clickable elements all share a common parent, you could just register the event handler on that, via event delegation, saving the need to register each button individually.






              share|improve this answer
































                0














                My proposition (es6) - "code from head":



                [
                "newFloorplan",
                "showOpenWindow",
                "saveFloorplan",
                "saveFloorplanAs",
                "showRemoveWindow",
                ].map(id=>document.getElementById(id).addEventListener('click', event=>
                filesystem[id]();
                event.preventDefault()
                ));





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322064%2fhow-to-simplify-multiple-functions-that-have-different-ids%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  2














                  Simply call wrapper function with dynamic values for ID and function name:



                  var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                  function bindEvent(id, callbackFunction)
                  document.getElementById(id).addEventListener('click', function (event))
                  event.preventDefault();

                  filesystem[callbackFunction]();



                  bindEvent('newFloorplan', 'newFloorplan');
                  bindEvent('showOpenWindow', 'showOpenWindow');
                  ...





                  share|improve this answer























                  • I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

                    – Grzesiek Danowski
                    Nov 15 '18 at 15:03











                  • @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

                    – Alnitak
                    Nov 15 '18 at 15:10











                  • Awesome, Thank You-

                    – Jon Nowak
                    Nov 15 '18 at 15:35











                  • Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

                    – Jon Nowak
                    Nov 15 '18 at 15:35
















                  2














                  Simply call wrapper function with dynamic values for ID and function name:



                  var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                  function bindEvent(id, callbackFunction)
                  document.getElementById(id).addEventListener('click', function (event))
                  event.preventDefault();

                  filesystem[callbackFunction]();



                  bindEvent('newFloorplan', 'newFloorplan');
                  bindEvent('showOpenWindow', 'showOpenWindow');
                  ...





                  share|improve this answer























                  • I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

                    – Grzesiek Danowski
                    Nov 15 '18 at 15:03











                  • @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

                    – Alnitak
                    Nov 15 '18 at 15:10











                  • Awesome, Thank You-

                    – Jon Nowak
                    Nov 15 '18 at 15:35











                  • Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

                    – Jon Nowak
                    Nov 15 '18 at 15:35














                  2












                  2








                  2







                  Simply call wrapper function with dynamic values for ID and function name:



                  var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                  function bindEvent(id, callbackFunction)
                  document.getElementById(id).addEventListener('click', function (event))
                  event.preventDefault();

                  filesystem[callbackFunction]();



                  bindEvent('newFloorplan', 'newFloorplan');
                  bindEvent('showOpenWindow', 'showOpenWindow');
                  ...





                  share|improve this answer













                  Simply call wrapper function with dynamic values for ID and function name:



                  var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                  function bindEvent(id, callbackFunction)
                  document.getElementById(id).addEventListener('click', function (event))
                  event.preventDefault();

                  filesystem[callbackFunction]();



                  bindEvent('newFloorplan', 'newFloorplan');
                  bindEvent('showOpenWindow', 'showOpenWindow');
                  ...






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 14:56









                  JustinasJustinas

                  27.7k33763




                  27.7k33763












                  • I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

                    – Grzesiek Danowski
                    Nov 15 '18 at 15:03











                  • @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

                    – Alnitak
                    Nov 15 '18 at 15:10











                  • Awesome, Thank You-

                    – Jon Nowak
                    Nov 15 '18 at 15:35











                  • Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

                    – Jon Nowak
                    Nov 15 '18 at 15:35


















                  • I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

                    – Grzesiek Danowski
                    Nov 15 '18 at 15:03











                  • @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

                    – Alnitak
                    Nov 15 '18 at 15:10











                  • Awesome, Thank You-

                    – Jon Nowak
                    Nov 15 '18 at 15:35











                  • Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

                    – Jon Nowak
                    Nov 15 '18 at 15:35

















                  I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

                  – Grzesiek Danowski
                  Nov 15 '18 at 15:03





                  I would prefer to use in bindEvent callback, not function name: use: callbackFunction(); and then: bindEvent('newFloorplan', filesystem.newFloorplan); In this way bindEvent will be more generic :).

                  – Grzesiek Danowski
                  Nov 15 '18 at 15:03













                  @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

                  – Alnitak
                  Nov 15 '18 at 15:10





                  @GrzesiekDanowski you'd need to use filesystem.newFloorplan.bind(filesystem) to make that work properly.

                  – Alnitak
                  Nov 15 '18 at 15:10













                  Awesome, Thank You-

                  – Jon Nowak
                  Nov 15 '18 at 15:35





                  Awesome, Thank You-

                  – Jon Nowak
                  Nov 15 '18 at 15:35













                  Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

                  – Jon Nowak
                  Nov 15 '18 at 15:35






                  Can I use this method for something like this: 'var ui = new fp.FloorplanUI(myFloorplan, "ui", "myFloorplan", fp.GUI_STATE); function bindEvent(id, floorplanUIFunction, div) document.getElementById(id).addEventListener('click', function (event) event.preventDefault(); ui[floorplanUIFunction]([div]); ); bindEvent('diagramHelpDivButton', 'hideShow', 'diagramHelpDiv');'

                  – Jon Nowak
                  Nov 15 '18 at 15:35














                  1














                  You can access the property of an object by a variable containing a string using bracket notation.






                  var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                  ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                  var el = document.getElementById(id);
                  el.addEventListener('click', function(event)
                  filesystem[id]();
                  event.preventDefault();
                  );
                  );








                  share|improve this answer



























                    1














                    You can access the property of an object by a variable containing a string using bracket notation.






                    var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                    ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                    var el = document.getElementById(id);
                    el.addEventListener('click', function(event)
                    filesystem[id]();
                    event.preventDefault();
                    );
                    );








                    share|improve this answer

























                      1












                      1








                      1







                      You can access the property of an object by a variable containing a string using bracket notation.






                      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                      ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                      var el = document.getElementById(id);
                      el.addEventListener('click', function(event)
                      filesystem[id]();
                      event.preventDefault();
                      );
                      );








                      share|improve this answer













                      You can access the property of an object by a variable containing a string using bracket notation.






                      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                      ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                      var el = document.getElementById(id);
                      el.addEventListener('click', function(event)
                      filesystem[id]();
                      event.preventDefault();
                      );
                      );








                      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                      ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                      var el = document.getElementById(id);
                      el.addEventListener('click', function(event)
                      filesystem[id]();
                      event.preventDefault();
                      );
                      );





                      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                      ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                      var el = document.getElementById(id);
                      el.addEventListener('click', function(event)
                      filesystem[id]();
                      event.preventDefault();
                      );
                      );






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 14:58









                      dgearedgeare

                      2,05731423




                      2,05731423





















                          0














                          const filesystem = new fp.FloorplanFilesystem (myFloorplan, fp.FILESYSTEM_UI_STATE)

                          const ids = ['newFloorplan', 'showOpenWindow', 'saveFloorplan', 'saveFloorplanAs', 'showRemoveWindow']
                          const handler = id => event => (filesystem [id] (), event.preventDefault ())

                          ids.forEach (id => document.getElementById (id).addEventListener ('click', handler (id)))





                          share|improve this answer



























                            0














                            const filesystem = new fp.FloorplanFilesystem (myFloorplan, fp.FILESYSTEM_UI_STATE)

                            const ids = ['newFloorplan', 'showOpenWindow', 'saveFloorplan', 'saveFloorplanAs', 'showRemoveWindow']
                            const handler = id => event => (filesystem [id] (), event.preventDefault ())

                            ids.forEach (id => document.getElementById (id).addEventListener ('click', handler (id)))





                            share|improve this answer

























                              0












                              0








                              0







                              const filesystem = new fp.FloorplanFilesystem (myFloorplan, fp.FILESYSTEM_UI_STATE)

                              const ids = ['newFloorplan', 'showOpenWindow', 'saveFloorplan', 'saveFloorplanAs', 'showRemoveWindow']
                              const handler = id => event => (filesystem [id] (), event.preventDefault ())

                              ids.forEach (id => document.getElementById (id).addEventListener ('click', handler (id)))





                              share|improve this answer













                              const filesystem = new fp.FloorplanFilesystem (myFloorplan, fp.FILESYSTEM_UI_STATE)

                              const ids = ['newFloorplan', 'showOpenWindow', 'saveFloorplan', 'saveFloorplanAs', 'showRemoveWindow']
                              const handler = id => event => (filesystem [id] (), event.preventDefault ())

                              ids.forEach (id => document.getElementById (id).addEventListener ('click', handler (id)))






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '18 at 15:02









                              leninelleninel

                              17914




                              17914





















                                  0














                                  Since your elements use the name of the function that you want to invoke as their ID, you can extract that within the event handler:



                                  var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                                  function fs_handler(event)
                                  filesystem[event.target.id]();
                                  event.preventDefault();


                                  ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                                  document.getElementById(id).addEventHandler('click', fs_handler);
                                  );


                                  This should be generally preferred over creating a new callback function object for each item.



                                  Furthermore, if the clickable elements all share a common parent, you could just register the event handler on that, via event delegation, saving the need to register each button individually.






                                  share|improve this answer





























                                    0














                                    Since your elements use the name of the function that you want to invoke as their ID, you can extract that within the event handler:



                                    var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                                    function fs_handler(event)
                                    filesystem[event.target.id]();
                                    event.preventDefault();


                                    ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                                    document.getElementById(id).addEventHandler('click', fs_handler);
                                    );


                                    This should be generally preferred over creating a new callback function object for each item.



                                    Furthermore, if the clickable elements all share a common parent, you could just register the event handler on that, via event delegation, saving the need to register each button individually.






                                    share|improve this answer



























                                      0












                                      0








                                      0







                                      Since your elements use the name of the function that you want to invoke as their ID, you can extract that within the event handler:



                                      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                                      function fs_handler(event)
                                      filesystem[event.target.id]();
                                      event.preventDefault();


                                      ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                                      document.getElementById(id).addEventHandler('click', fs_handler);
                                      );


                                      This should be generally preferred over creating a new callback function object for each item.



                                      Furthermore, if the clickable elements all share a common parent, you could just register the event handler on that, via event delegation, saving the need to register each button individually.






                                      share|improve this answer















                                      Since your elements use the name of the function that you want to invoke as their ID, you can extract that within the event handler:



                                      var filesystem = new fp.FloorplanFilesystem(myFloorplan, fp.FILESYSTEM_UI_STATE);

                                      function fs_handler(event)
                                      filesystem[event.target.id]();
                                      event.preventDefault();


                                      ["newFloorplan", "showOpenWindow", "saveFloorplan", "saveFloorplanAs", "showRemoveWindow"].forEach(function(id)
                                      document.getElementById(id).addEventHandler('click', fs_handler);
                                      );


                                      This should be generally preferred over creating a new callback function object for each item.



                                      Furthermore, if the clickable elements all share a common parent, you could just register the event handler on that, via event delegation, saving the need to register each button individually.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 15 '18 at 15:22

























                                      answered Nov 15 '18 at 15:03









                                      AlnitakAlnitak

                                      275k62348437




                                      275k62348437





















                                          0














                                          My proposition (es6) - "code from head":



                                          [
                                          "newFloorplan",
                                          "showOpenWindow",
                                          "saveFloorplan",
                                          "saveFloorplanAs",
                                          "showRemoveWindow",
                                          ].map(id=>document.getElementById(id).addEventListener('click', event=>
                                          filesystem[id]();
                                          event.preventDefault()
                                          ));





                                          share|improve this answer



























                                            0














                                            My proposition (es6) - "code from head":



                                            [
                                            "newFloorplan",
                                            "showOpenWindow",
                                            "saveFloorplan",
                                            "saveFloorplanAs",
                                            "showRemoveWindow",
                                            ].map(id=>document.getElementById(id).addEventListener('click', event=>
                                            filesystem[id]();
                                            event.preventDefault()
                                            ));





                                            share|improve this answer

























                                              0












                                              0








                                              0







                                              My proposition (es6) - "code from head":



                                              [
                                              "newFloorplan",
                                              "showOpenWindow",
                                              "saveFloorplan",
                                              "saveFloorplanAs",
                                              "showRemoveWindow",
                                              ].map(id=>document.getElementById(id).addEventListener('click', event=>
                                              filesystem[id]();
                                              event.preventDefault()
                                              ));





                                              share|improve this answer













                                              My proposition (es6) - "code from head":



                                              [
                                              "newFloorplan",
                                              "showOpenWindow",
                                              "saveFloorplan",
                                              "saveFloorplanAs",
                                              "showRemoveWindow",
                                              ].map(id=>document.getElementById(id).addEventListener('click', event=>
                                              filesystem[id]();
                                              event.preventDefault()
                                              ));






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 1 '18 at 19:26









                                              Kamil KiełczewskiKamil Kiełczewski

                                              14.4k87397




                                              14.4k87397



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322064%2fhow-to-simplify-multiple-functions-that-have-different-ids%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                                  Syphilis

                                                  Darth Vader #20