Buttons that expand or collapse all the within the document

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
0
down vote

favorite












I have <button>s that when pressed expand or collapse the contents of all the <details> within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.






expandDetBtn = document.getElementById("showBtn");
collapseDetBtn = document.getElementById("hideBtn");

expandDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open == false)
x[i].setAttribute("open", "true");


);

collapseDetBtn.addEventListener("click", function expandDetails(x)
var x = document.getElementsByTagName("details");
var i;
var len = x.length;
for (i = 0; i < len; i++)
if (x[i].open)
x[i].removeAttribute("open");


);

<button id="showBtn">Show details</button>
<button id="hideBtn">Hide details</button>

<details>
<summary>Section 1</summary>
<p>Text 1</p>
</details>

<details>
<summary>Section 2</summary>
<p>Text 2</p>
</details>

<details>
<summary>Section 3</summary>
<p>Text 3</p>
</details>









share|improve this question

























    up vote
    0
    down vote

    favorite












    I have <button>s that when pressed expand or collapse the contents of all the <details> within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.






    expandDetBtn = document.getElementById("showBtn");
    collapseDetBtn = document.getElementById("hideBtn");

    expandDetBtn.addEventListener("click", function expandDetails(x)
    var x = document.getElementsByTagName("details");
    var i;
    var len = x.length;
    for (i = 0; i < len; i++)
    if (x[i].open == false)
    x[i].setAttribute("open", "true");


    );

    collapseDetBtn.addEventListener("click", function expandDetails(x)
    var x = document.getElementsByTagName("details");
    var i;
    var len = x.length;
    for (i = 0; i < len; i++)
    if (x[i].open)
    x[i].removeAttribute("open");


    );

    <button id="showBtn">Show details</button>
    <button id="hideBtn">Hide details</button>

    <details>
    <summary>Section 1</summary>
    <p>Text 1</p>
    </details>

    <details>
    <summary>Section 2</summary>
    <p>Text 2</p>
    </details>

    <details>
    <summary>Section 3</summary>
    <p>Text 3</p>
    </details>









    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have <button>s that when pressed expand or collapse the contents of all the <details> within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.






      expandDetBtn = document.getElementById("showBtn");
      collapseDetBtn = document.getElementById("hideBtn");

      expandDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open == false)
      x[i].setAttribute("open", "true");


      );

      collapseDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open)
      x[i].removeAttribute("open");


      );

      <button id="showBtn">Show details</button>
      <button id="hideBtn">Hide details</button>

      <details>
      <summary>Section 1</summary>
      <p>Text 1</p>
      </details>

      <details>
      <summary>Section 2</summary>
      <p>Text 2</p>
      </details>

      <details>
      <summary>Section 3</summary>
      <p>Text 3</p>
      </details>









      share|improve this question











      I have <button>s that when pressed expand or collapse the contents of all the <details> within the document. The code works, but as a novice in JavaScript, I am not very confident in its performance and/or efficiency. Could you please review it.






      expandDetBtn = document.getElementById("showBtn");
      collapseDetBtn = document.getElementById("hideBtn");

      expandDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open == false)
      x[i].setAttribute("open", "true");


      );

      collapseDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open)
      x[i].removeAttribute("open");


      );

      <button id="showBtn">Show details</button>
      <button id="hideBtn">Hide details</button>

      <details>
      <summary>Section 1</summary>
      <p>Text 1</p>
      </details>

      <details>
      <summary>Section 2</summary>
      <p>Text 2</p>
      </details>

      <details>
      <summary>Section 3</summary>
      <p>Text 3</p>
      </details>








      expandDetBtn = document.getElementById("showBtn");
      collapseDetBtn = document.getElementById("hideBtn");

      expandDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open == false)
      x[i].setAttribute("open", "true");


      );

      collapseDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open)
      x[i].removeAttribute("open");


      );

      <button id="showBtn">Show details</button>
      <button id="hideBtn">Hide details</button>

      <details>
      <summary>Section 1</summary>
      <p>Text 1</p>
      </details>

      <details>
      <summary>Section 2</summary>
      <p>Text 2</p>
      </details>

      <details>
      <summary>Section 3</summary>
      <p>Text 3</p>
      </details>





      expandDetBtn = document.getElementById("showBtn");
      collapseDetBtn = document.getElementById("hideBtn");

      expandDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open == false)
      x[i].setAttribute("open", "true");


      );

      collapseDetBtn.addEventListener("click", function expandDetails(x)
      var x = document.getElementsByTagName("details");
      var i;
      var len = x.length;
      for (i = 0; i < len; i++)
      if (x[i].open)
      x[i].removeAttribute("open");


      );

      <button id="showBtn">Show details</button>
      <button id="hideBtn">Hide details</button>

      <details>
      <summary>Section 1</summary>
      <p>Text 1</p>
      </details>

      <details>
      <summary>Section 2</summary>
      <p>Text 2</p>
      </details>

      <details>
      <summary>Section 3</summary>
      <p>Text 3</p>
      </details>








      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 15 at 19:50









      JAT86

      1224




      1224




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.



          You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.



          You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x parameter.



          Don't use var, use let and const instead.



          x is not a very descriptive name. It also overwrites the exiting parameter.



          There is no need to save the length in a variable. You also don't need to declare i before the loop, since it's not used outside.



          There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.



          Don't use "true" here .setAttribute("open", "true");. According to this.




          Boolean attributes are considered to be true if they're present on the
          element at all, regardless of their actual value; as a rule, you
          should specify the empty string ("") in value




          The outcome is the same, but it might lead people to believe that setting the value to "false" will remove it, which is not the case.



          With all these changes, the opening look like this



          expandDetBtn.addEventListener("click", function() 
          const elements = document.getElementsByTagName("details");
          for (let i = 0; i < elements.length; i++)
          elements[i].setAttribute("open", "");

          );


          If you want to get more advanced you could use a single function, since they are very similar.



          function toggleDetails(open) 
          for(element of document.getElementsByTagName("details"))
          element.open = open;



          expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
          collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));





          share|improve this answer























          • <detail> elements have an open property, so it should really just be element.open = open
            – Gerrit0
            Apr 16 at 14:58










          • @Gerrit0 you are right, didn't even think about that. Thanks.
            – Kruga
            Apr 16 at 15:23










          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%2f192138%2fbuttons-that-expand-or-collapse-all-the-details-within-the-document%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.



          You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.



          You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x parameter.



          Don't use var, use let and const instead.



          x is not a very descriptive name. It also overwrites the exiting parameter.



          There is no need to save the length in a variable. You also don't need to declare i before the loop, since it's not used outside.



          There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.



          Don't use "true" here .setAttribute("open", "true");. According to this.




          Boolean attributes are considered to be true if they're present on the
          element at all, regardless of their actual value; as a rule, you
          should specify the empty string ("") in value




          The outcome is the same, but it might lead people to believe that setting the value to "false" will remove it, which is not the case.



          With all these changes, the opening look like this



          expandDetBtn.addEventListener("click", function() 
          const elements = document.getElementsByTagName("details");
          for (let i = 0; i < elements.length; i++)
          elements[i].setAttribute("open", "");

          );


          If you want to get more advanced you could use a single function, since they are very similar.



          function toggleDetails(open) 
          for(element of document.getElementsByTagName("details"))
          element.open = open;



          expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
          collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));





          share|improve this answer























          • <detail> elements have an open property, so it should really just be element.open = open
            – Gerrit0
            Apr 16 at 14:58










          • @Gerrit0 you are right, didn't even think about that. Thanks.
            – Kruga
            Apr 16 at 15:23














          up vote
          1
          down vote



          accepted










          Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.



          You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.



          You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x parameter.



          Don't use var, use let and const instead.



          x is not a very descriptive name. It also overwrites the exiting parameter.



          There is no need to save the length in a variable. You also don't need to declare i before the loop, since it's not used outside.



          There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.



          Don't use "true" here .setAttribute("open", "true");. According to this.




          Boolean attributes are considered to be true if they're present on the
          element at all, regardless of their actual value; as a rule, you
          should specify the empty string ("") in value




          The outcome is the same, but it might lead people to believe that setting the value to "false" will remove it, which is not the case.



          With all these changes, the opening look like this



          expandDetBtn.addEventListener("click", function() 
          const elements = document.getElementsByTagName("details");
          for (let i = 0; i < elements.length; i++)
          elements[i].setAttribute("open", "");

          );


          If you want to get more advanced you could use a single function, since they are very similar.



          function toggleDetails(open) 
          for(element of document.getElementsByTagName("details"))
          element.open = open;



          expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
          collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));





          share|improve this answer























          • <detail> elements have an open property, so it should really just be element.open = open
            – Gerrit0
            Apr 16 at 14:58










          • @Gerrit0 you are right, didn't even think about that. Thanks.
            – Kruga
            Apr 16 at 15:23












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.



          You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.



          You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x parameter.



          Don't use var, use let and const instead.



          x is not a very descriptive name. It also overwrites the exiting parameter.



          There is no need to save the length in a variable. You also don't need to declare i before the loop, since it's not used outside.



          There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.



          Don't use "true" here .setAttribute("open", "true");. According to this.




          Boolean attributes are considered to be true if they're present on the
          element at all, regardless of their actual value; as a rule, you
          should specify the empty string ("") in value




          The outcome is the same, but it might lead people to believe that setting the value to "false" will remove it, which is not the case.



          With all these changes, the opening look like this



          expandDetBtn.addEventListener("click", function() 
          const elements = document.getElementsByTagName("details");
          for (let i = 0; i < elements.length; i++)
          elements[i].setAttribute("open", "");

          );


          If you want to get more advanced you could use a single function, since they are very similar.



          function toggleDetails(open) 
          for(element of document.getElementsByTagName("details"))
          element.open = open;



          expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
          collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));





          share|improve this answer















          Performance-wise the code is fine. There is not really anything to improve. I have a few other comments about the code.



          You are accessing DOM elements at the start, without checking if they are loaded. Usually they are not, which would cause an error here.



          You are naming your handler function, but never refer to them. You can just use an anonymous function here. You also don't use the x parameter.



          Don't use var, use let and const instead.



          x is not a very descriptive name. It also overwrites the exiting parameter.



          There is no need to save the length in a variable. You also don't need to declare i before the loop, since it's not used outside.



          There is no need to check if they are already opened or closed. Opening one which is already open doesn't do anything, so just open them all.



          Don't use "true" here .setAttribute("open", "true");. According to this.




          Boolean attributes are considered to be true if they're present on the
          element at all, regardless of their actual value; as a rule, you
          should specify the empty string ("") in value




          The outcome is the same, but it might lead people to believe that setting the value to "false" will remove it, which is not the case.



          With all these changes, the opening look like this



          expandDetBtn.addEventListener("click", function() 
          const elements = document.getElementsByTagName("details");
          for (let i = 0; i < elements.length; i++)
          elements[i].setAttribute("open", "");

          );


          If you want to get more advanced you could use a single function, since they are very similar.



          function toggleDetails(open) 
          for(element of document.getElementsByTagName("details"))
          element.open = open;



          expandDetBtn.addEventListener("click", toggleDetails.bind(null, true));
          collapseDetBtn.addEventListener("click", toggleDetails.bind(null, false));






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Apr 16 at 15:20


























          answered Apr 16 at 13:14









          Kruga

          74819




          74819











          • <detail> elements have an open property, so it should really just be element.open = open
            – Gerrit0
            Apr 16 at 14:58










          • @Gerrit0 you are right, didn't even think about that. Thanks.
            – Kruga
            Apr 16 at 15:23
















          • <detail> elements have an open property, so it should really just be element.open = open
            – Gerrit0
            Apr 16 at 14:58










          • @Gerrit0 you are right, didn't even think about that. Thanks.
            – Kruga
            Apr 16 at 15:23















          <detail> elements have an open property, so it should really just be element.open = open
          – Gerrit0
          Apr 16 at 14:58




          <detail> elements have an open property, so it should really just be element.open = open
          – Gerrit0
          Apr 16 at 14:58












          @Gerrit0 you are right, didn't even think about that. Thanks.
          – Kruga
          Apr 16 at 15:23




          @Gerrit0 you are right, didn't even think about that. Thanks.
          – Kruga
          Apr 16 at 15:23












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192138%2fbuttons-that-expand-or-collapse-all-the-details-within-the-document%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?