JavaScript toggle form elements

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.



My question comes to making this code more concise and deliberate in a simple toggle function:



function toggleFormElements(toggle, elArr) 
if (toggle == 'disable')
elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
elArr.btn.setAttribute('disabled', 'disabled');

// inputs
elArr.fields.forEach((field, i) =>
field.setAttribute('disabled', 'disabled');
);
else if (toggle == 'enable')
elArr.btn.innerHTML = 'Submit';
elArr.btn.removeAttribute('disabled');

// inputs
elArr.fields.forEach((field, i) =>
field.removeAttribute('disabled');
);




Would it be possible to make this code more readable or simply shorter?







share|improve this question

























    up vote
    3
    down vote

    favorite












    Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.



    My question comes to making this code more concise and deliberate in a simple toggle function:



    function toggleFormElements(toggle, elArr) 
    if (toggle == 'disable')
    elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
    elArr.btn.setAttribute('disabled', 'disabled');

    // inputs
    elArr.fields.forEach((field, i) =>
    field.setAttribute('disabled', 'disabled');
    );
    else if (toggle == 'enable')
    elArr.btn.innerHTML = 'Submit';
    elArr.btn.removeAttribute('disabled');

    // inputs
    elArr.fields.forEach((field, i) =>
    field.removeAttribute('disabled');
    );




    Would it be possible to make this code more readable or simply shorter?







    share|improve this question





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.



      My question comes to making this code more concise and deliberate in a simple toggle function:



      function toggleFormElements(toggle, elArr) 
      if (toggle == 'disable')
      elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
      elArr.btn.setAttribute('disabled', 'disabled');

      // inputs
      elArr.fields.forEach((field, i) =>
      field.setAttribute('disabled', 'disabled');
      );
      else if (toggle == 'enable')
      elArr.btn.innerHTML = 'Submit';
      elArr.btn.removeAttribute('disabled');

      // inputs
      elArr.fields.forEach((field, i) =>
      field.removeAttribute('disabled');
      );




      Would it be possible to make this code more readable or simply shorter?







      share|improve this question











      Fellow JS devs, I have come across a fairly simple question which has puzzled me. When the user submits a form I disable all inputs and submit button changes to a loading animation (fa-spinner from fontawesome). Once the form submit is complete then I restore the previous state, and enable everything back.



      My question comes to making this code more concise and deliberate in a simple toggle function:



      function toggleFormElements(toggle, elArr) 
      if (toggle == 'disable')
      elArr.btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
      elArr.btn.setAttribute('disabled', 'disabled');

      // inputs
      elArr.fields.forEach((field, i) =>
      field.setAttribute('disabled', 'disabled');
      );
      else if (toggle == 'enable')
      elArr.btn.innerHTML = 'Submit';
      elArr.btn.removeAttribute('disabled');

      // inputs
      elArr.fields.forEach((field, i) =>
      field.removeAttribute('disabled');
      );




      Would it be possible to make this code more readable or simply shorter?









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 8 at 18:59









      Samuel

      1639




      1639




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          arr.forEach



          I would consider using a simple for loop to iterate over the elArr (perhaps consider a bit better name, like elementsArray or just elements). This StackOverflow post goes over some of the benefits of doing this.



          You can also use the disabled attribute directly.



          for (let i = 0; i < elArr.length; i++) 
          elArr[i].disabled = true; // or false to remove it
          );


          I would also personally prefer to have a boolean argument like isToggled instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).



          if (isToggled) 
          ...
          else
          ...



          The comment // inputs doesn't really achieve anything, I would suggest to remove it completely.






          share|improve this answer





















          • Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
            – Samuel
            May 9 at 1:05











          • That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
            – Samuel
            May 9 at 1:06

















          up vote
          3
          down vote













          That's not a toggle



          • Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of setFormDisableState(formElements, disable = true) or setFormState(formElements, disable)

          Argument order



          • If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for a = b. Your function is assigning a state to elements in the object elArr, the elArr should be to the left of the toggle.

          Danger! implied type




          • elArr is not an array. That is most defiantly a bad name as it implies the incorrect type.

          The shortest form.



          • For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.

          Keep the source DRY



          • Don't Repeat Yourself (code). The field iterators elArr.fields.forEach are almost identical, with only the state changing.


          • You have two if statements. You don't need to do the second test. If not disabling then must be enabling.


          Alternatives



          function toggleFormState(formDesc) // Desc is short for description
          const newState = ! formDesc.btn.disabled;
          formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = newState;
          for (const element of formDesc.fields) element.disabled = newState



          Or



          function setFormState(formDesc, disable) 
          formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = disable;
          for (const element of formDesc.fields) element.disabled = disable






          share|improve this answer





















          • nice use of for...of in the alternatives
            – Sam Onela
            Jun 14 at 20:35











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193946%2fjavascript-toggle-form-elements%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          arr.forEach



          I would consider using a simple for loop to iterate over the elArr (perhaps consider a bit better name, like elementsArray or just elements). This StackOverflow post goes over some of the benefits of doing this.



          You can also use the disabled attribute directly.



          for (let i = 0; i < elArr.length; i++) 
          elArr[i].disabled = true; // or false to remove it
          );


          I would also personally prefer to have a boolean argument like isToggled instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).



          if (isToggled) 
          ...
          else
          ...



          The comment // inputs doesn't really achieve anything, I would suggest to remove it completely.






          share|improve this answer





















          • Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
            – Samuel
            May 9 at 1:05











          • That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
            – Samuel
            May 9 at 1:06














          up vote
          2
          down vote



          accepted










          arr.forEach



          I would consider using a simple for loop to iterate over the elArr (perhaps consider a bit better name, like elementsArray or just elements). This StackOverflow post goes over some of the benefits of doing this.



          You can also use the disabled attribute directly.



          for (let i = 0; i < elArr.length; i++) 
          elArr[i].disabled = true; // or false to remove it
          );


          I would also personally prefer to have a boolean argument like isToggled instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).



          if (isToggled) 
          ...
          else
          ...



          The comment // inputs doesn't really achieve anything, I would suggest to remove it completely.






          share|improve this answer





















          • Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
            – Samuel
            May 9 at 1:05











          • That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
            – Samuel
            May 9 at 1:06












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          arr.forEach



          I would consider using a simple for loop to iterate over the elArr (perhaps consider a bit better name, like elementsArray or just elements). This StackOverflow post goes over some of the benefits of doing this.



          You can also use the disabled attribute directly.



          for (let i = 0; i < elArr.length; i++) 
          elArr[i].disabled = true; // or false to remove it
          );


          I would also personally prefer to have a boolean argument like isToggled instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).



          if (isToggled) 
          ...
          else
          ...



          The comment // inputs doesn't really achieve anything, I would suggest to remove it completely.






          share|improve this answer













          arr.forEach



          I would consider using a simple for loop to iterate over the elArr (perhaps consider a bit better name, like elementsArray or just elements). This StackOverflow post goes over some of the benefits of doing this.



          You can also use the disabled attribute directly.



          for (let i = 0; i < elArr.length; i++) 
          elArr[i].disabled = true; // or false to remove it
          );


          I would also personally prefer to have a boolean argument like isToggled instead of expecting a string, so you can do something like below. In my opinion a boolean is less likely to result in an error (e.g. a misspelled string).



          if (isToggled) 
          ...
          else
          ...



          The comment // inputs doesn't really achieve anything, I would suggest to remove it completely.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 9 at 0:56









          Phrancis

          14.6k644137




          14.6k644137











          • Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
            – Samuel
            May 9 at 1:05











          • That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
            – Samuel
            May 9 at 1:06
















          • Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
            – Samuel
            May 9 at 1:05











          • That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
            – Samuel
            May 9 at 1:06















          Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
          – Samuel
          May 9 at 1:05





          Haha yes the // inputs comment was just for this question. I don't actually have it on there 😁
          – Samuel
          May 9 at 1:05













          That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
          – Samuel
          May 9 at 1:06




          That's for the answer, it actually makes a lot of sense, especially with the bool vs a string
          – Samuel
          May 9 at 1:06












          up vote
          3
          down vote













          That's not a toggle



          • Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of setFormDisableState(formElements, disable = true) or setFormState(formElements, disable)

          Argument order



          • If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for a = b. Your function is assigning a state to elements in the object elArr, the elArr should be to the left of the toggle.

          Danger! implied type




          • elArr is not an array. That is most defiantly a bad name as it implies the incorrect type.

          The shortest form.



          • For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.

          Keep the source DRY



          • Don't Repeat Yourself (code). The field iterators elArr.fields.forEach are almost identical, with only the state changing.


          • You have two if statements. You don't need to do the second test. If not disabling then must be enabling.


          Alternatives



          function toggleFormState(formDesc) // Desc is short for description
          const newState = ! formDesc.btn.disabled;
          formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = newState;
          for (const element of formDesc.fields) element.disabled = newState



          Or



          function setFormState(formDesc, disable) 
          formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = disable;
          for (const element of formDesc.fields) element.disabled = disable






          share|improve this answer





















          • nice use of for...of in the alternatives
            – Sam Onela
            Jun 14 at 20:35















          up vote
          3
          down vote













          That's not a toggle



          • Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of setFormDisableState(formElements, disable = true) or setFormState(formElements, disable)

          Argument order



          • If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for a = b. Your function is assigning a state to elements in the object elArr, the elArr should be to the left of the toggle.

          Danger! implied type




          • elArr is not an array. That is most defiantly a bad name as it implies the incorrect type.

          The shortest form.



          • For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.

          Keep the source DRY



          • Don't Repeat Yourself (code). The field iterators elArr.fields.forEach are almost identical, with only the state changing.


          • You have two if statements. You don't need to do the second test. If not disabling then must be enabling.


          Alternatives



          function toggleFormState(formDesc) // Desc is short for description
          const newState = ! formDesc.btn.disabled;
          formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = newState;
          for (const element of formDesc.fields) element.disabled = newState



          Or



          function setFormState(formDesc, disable) 
          formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = disable;
          for (const element of formDesc.fields) element.disabled = disable






          share|improve this answer





















          • nice use of for...of in the alternatives
            – Sam Onela
            Jun 14 at 20:35













          up vote
          3
          down vote










          up vote
          3
          down vote









          That's not a toggle



          • Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of setFormDisableState(formElements, disable = true) or setFormState(formElements, disable)

          Argument order



          • If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for a = b. Your function is assigning a state to elements in the object elArr, the elArr should be to the left of the toggle.

          Danger! implied type




          • elArr is not an array. That is most defiantly a bad name as it implies the incorrect type.

          The shortest form.



          • For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.

          Keep the source DRY



          • Don't Repeat Yourself (code). The field iterators elArr.fields.forEach are almost identical, with only the state changing.


          • You have two if statements. You don't need to do the second test. If not disabling then must be enabling.


          Alternatives



          function toggleFormState(formDesc) // Desc is short for description
          const newState = ! formDesc.btn.disabled;
          formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = newState;
          for (const element of formDesc.fields) element.disabled = newState



          Or



          function setFormState(formDesc, disable) 
          formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = disable;
          for (const element of formDesc.fields) element.disabled = disable






          share|improve this answer













          That's not a toggle



          • Toggle has a very specific meaning and comes with the benefit of not having to supply the new state when called. Your function is not a toggle and should be called something along the lines of setFormDisableState(formElements, disable = true) or setFormState(formElements, disable)

          Argument order



          • If not using a default parameter use the right to left assignment rule when defining argument order. Eg assignment is right to left for a = b. Your function is assigning a state to elements in the object elArr, the elArr should be to the left of the toggle.

          Danger! implied type




          • elArr is not an array. That is most defiantly a bad name as it implies the incorrect type.

          The shortest form.



          • For all attributes defined in the DOM set the values directly. You only use the attribute functions for attributes not defined by the DOM. Using the longer version to do anything in code adds unneeded noise.

          Keep the source DRY



          • Don't Repeat Yourself (code). The field iterators elArr.fields.forEach are almost identical, with only the state changing.


          • You have two if statements. You don't need to do the second test. If not disabling then must be enabling.


          Alternatives



          function toggleFormState(formDesc) // Desc is short for description
          const newState = ! formDesc.btn.disabled;
          formDesc.btn.innerHTML = newState ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = newState;
          for (const element of formDesc.fields) element.disabled = newState



          Or



          function setFormState(formDesc, disable) 
          formDesc.btn.innerHTML = disable ? '<i class="fas fa-spinner fa-spin"></i>' : "Submit";
          formDesc.btn.disabled = disable;
          for (const element of formDesc.fields) element.disabled = disable







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 9 at 7:43









          Blindman67

          5,3111320




          5,3111320











          • nice use of for...of in the alternatives
            – Sam Onela
            Jun 14 at 20:35

















          • nice use of for...of in the alternatives
            – Sam Onela
            Jun 14 at 20:35
















          nice use of for...of in the alternatives
          – Sam Onela
          Jun 14 at 20:35





          nice use of for...of in the alternatives
          – Sam Onela
          Jun 14 at 20:35













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193946%2fjavascript-toggle-form-elements%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?