Show/hide on checkbox click does not work consistently












1















I am building an inspection app to walk an inspector through a house with context-relevant questions based on answers. I am having inconsistent success with using checkboxes to show/hide div content.



I have simplified my problem down to a few very basic lines of code that I can't seem to troubleshoot.



<input type="checkbox" name="cbReplaceOrRepair_0" value="Replace" id="cbReplaceOrRepair_0" onclick="showReplaceRoof()">Replace
<div class="dvRoofReplace" id="dvReplaceRoof" style="display:none">"Replace" option is checked</div><br>
<input type="checkbox" name="cbReplaceOrRepair_1" value="Repair" id="cbReplaceOrRepair_1" onclick="showDvConfirmRepairability()">Repair
<div class="dvRoofRepair" id="dvConfirmRepairability" style="display:none">"Repair" option is checked</div>


<script type="text/javascript">
function showReplaceRoof() {
var dvReplaceRoof = document.getElementById("dvReplaceRoof");
dvReplaceRoof.style.display = cbReplaceOrRepair_0.checked ? "block" : "none";
}
function showDvConfirmRepairability() {
var dvRoofRepair = document.getElementById("dvRoofRepair");
dvRoofRepair.style.display = cbReplaceOrRepair_1.checked ? "block" : "none";
}
</script>


My example shows one checkbox that works to show/hide a div and another on that does not work. Can anyone tell me what I'm doing wrong?



Also, this is my first question on here after literally years of looking up questions so I'll apologize in advance in case I messed up any of the posting protocols.



https://codepen.io/stephenskrocki/pen/EGoYaB










share|improve this question



























    1















    I am building an inspection app to walk an inspector through a house with context-relevant questions based on answers. I am having inconsistent success with using checkboxes to show/hide div content.



    I have simplified my problem down to a few very basic lines of code that I can't seem to troubleshoot.



    <input type="checkbox" name="cbReplaceOrRepair_0" value="Replace" id="cbReplaceOrRepair_0" onclick="showReplaceRoof()">Replace
    <div class="dvRoofReplace" id="dvReplaceRoof" style="display:none">"Replace" option is checked</div><br>
    <input type="checkbox" name="cbReplaceOrRepair_1" value="Repair" id="cbReplaceOrRepair_1" onclick="showDvConfirmRepairability()">Repair
    <div class="dvRoofRepair" id="dvConfirmRepairability" style="display:none">"Repair" option is checked</div>


    <script type="text/javascript">
    function showReplaceRoof() {
    var dvReplaceRoof = document.getElementById("dvReplaceRoof");
    dvReplaceRoof.style.display = cbReplaceOrRepair_0.checked ? "block" : "none";
    }
    function showDvConfirmRepairability() {
    var dvRoofRepair = document.getElementById("dvRoofRepair");
    dvRoofRepair.style.display = cbReplaceOrRepair_1.checked ? "block" : "none";
    }
    </script>


    My example shows one checkbox that works to show/hide a div and another on that does not work. Can anyone tell me what I'm doing wrong?



    Also, this is my first question on here after literally years of looking up questions so I'll apologize in advance in case I messed up any of the posting protocols.



    https://codepen.io/stephenskrocki/pen/EGoYaB










    share|improve this question

























      1












      1








      1








      I am building an inspection app to walk an inspector through a house with context-relevant questions based on answers. I am having inconsistent success with using checkboxes to show/hide div content.



      I have simplified my problem down to a few very basic lines of code that I can't seem to troubleshoot.



      <input type="checkbox" name="cbReplaceOrRepair_0" value="Replace" id="cbReplaceOrRepair_0" onclick="showReplaceRoof()">Replace
      <div class="dvRoofReplace" id="dvReplaceRoof" style="display:none">"Replace" option is checked</div><br>
      <input type="checkbox" name="cbReplaceOrRepair_1" value="Repair" id="cbReplaceOrRepair_1" onclick="showDvConfirmRepairability()">Repair
      <div class="dvRoofRepair" id="dvConfirmRepairability" style="display:none">"Repair" option is checked</div>


      <script type="text/javascript">
      function showReplaceRoof() {
      var dvReplaceRoof = document.getElementById("dvReplaceRoof");
      dvReplaceRoof.style.display = cbReplaceOrRepair_0.checked ? "block" : "none";
      }
      function showDvConfirmRepairability() {
      var dvRoofRepair = document.getElementById("dvRoofRepair");
      dvRoofRepair.style.display = cbReplaceOrRepair_1.checked ? "block" : "none";
      }
      </script>


      My example shows one checkbox that works to show/hide a div and another on that does not work. Can anyone tell me what I'm doing wrong?



      Also, this is my first question on here after literally years of looking up questions so I'll apologize in advance in case I messed up any of the posting protocols.



      https://codepen.io/stephenskrocki/pen/EGoYaB










      share|improve this question














      I am building an inspection app to walk an inspector through a house with context-relevant questions based on answers. I am having inconsistent success with using checkboxes to show/hide div content.



      I have simplified my problem down to a few very basic lines of code that I can't seem to troubleshoot.



      <input type="checkbox" name="cbReplaceOrRepair_0" value="Replace" id="cbReplaceOrRepair_0" onclick="showReplaceRoof()">Replace
      <div class="dvRoofReplace" id="dvReplaceRoof" style="display:none">"Replace" option is checked</div><br>
      <input type="checkbox" name="cbReplaceOrRepair_1" value="Repair" id="cbReplaceOrRepair_1" onclick="showDvConfirmRepairability()">Repair
      <div class="dvRoofRepair" id="dvConfirmRepairability" style="display:none">"Repair" option is checked</div>


      <script type="text/javascript">
      function showReplaceRoof() {
      var dvReplaceRoof = document.getElementById("dvReplaceRoof");
      dvReplaceRoof.style.display = cbReplaceOrRepair_0.checked ? "block" : "none";
      }
      function showDvConfirmRepairability() {
      var dvRoofRepair = document.getElementById("dvRoofRepair");
      dvRoofRepair.style.display = cbReplaceOrRepair_1.checked ? "block" : "none";
      }
      </script>


      My example shows one checkbox that works to show/hide a div and another on that does not work. Can anyone tell me what I'm doing wrong?



      Also, this is my first question on here after literally years of looking up questions so I'll apologize in advance in case I messed up any of the posting protocols.



      https://codepen.io/stephenskrocki/pen/EGoYaB







      javascript html






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 19:37









      FunctionalHippieFunctionalHippie

      82




      82
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You access the element incorrectly.



          The id of the second hidden element is id="dvConfirmRepairability.



          Use var dvRoofRepair = document.getElementById("dvConfirmRepairability");



          Hope this helps!!






          share|improve this answer
























          • Thanks that worked perfectly!!

            – FunctionalHippie
            Jan 1 at 23:26











          • @FunctionalHippie No problem!

            – Da Mahdi03
            Jan 2 at 2:30



















          0














          Since there is only one wanted option, instead of checkbox I would use radio buttons:



          HTML Markup:



          <style>label > span {display: none;} /* hide notes by default */</style>
          <label for="replace">Replace
          <input type="radio" name="radio" id="replace" onchange="radio()" />
          <span class="note">"Replace" option is checked</span>
          </label>
          <label for="repair">Repair
          <input type="radio" name="radio" id="repair" onchange="radio()" />
          <span class="note">"Repair" option is checked</span>
          </label>


          javaScript:



          <script>
          function radio() {
          var myRadio = document.querySelectorAll('input[name="radio"]'); // get radio
          var myNotes = document.querySelectorAll('.note'); // get notes
          if (myRadio[0].checked === true) { // if the first radio checked
          myNotes[0].style.display = 'inline-block'; // show first note
          myNotes[1].style.display = 'none'; // hide the second
          }
          else { // if not do the apposite
          myNotes[0].style.display = 'none';
          myNotes[1].style.display = 'inline-block';
          }
          }
          </script>


          Hope it help you!






          share|improve this answer


























          • I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

            – FunctionalHippie
            Jan 1 at 23:16













          • Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

            – A. Meshu
            Jan 2 at 22:41






          • 1





            Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

            – FunctionalHippie
            Jan 3 at 1:21











          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%2f53998378%2fshow-hide-on-checkbox-click-does-not-work-consistently%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You access the element incorrectly.



          The id of the second hidden element is id="dvConfirmRepairability.



          Use var dvRoofRepair = document.getElementById("dvConfirmRepairability");



          Hope this helps!!






          share|improve this answer
























          • Thanks that worked perfectly!!

            – FunctionalHippie
            Jan 1 at 23:26











          • @FunctionalHippie No problem!

            – Da Mahdi03
            Jan 2 at 2:30
















          0














          You access the element incorrectly.



          The id of the second hidden element is id="dvConfirmRepairability.



          Use var dvRoofRepair = document.getElementById("dvConfirmRepairability");



          Hope this helps!!






          share|improve this answer
























          • Thanks that worked perfectly!!

            – FunctionalHippie
            Jan 1 at 23:26











          • @FunctionalHippie No problem!

            – Da Mahdi03
            Jan 2 at 2:30














          0












          0








          0







          You access the element incorrectly.



          The id of the second hidden element is id="dvConfirmRepairability.



          Use var dvRoofRepair = document.getElementById("dvConfirmRepairability");



          Hope this helps!!






          share|improve this answer













          You access the element incorrectly.



          The id of the second hidden element is id="dvConfirmRepairability.



          Use var dvRoofRepair = document.getElementById("dvConfirmRepairability");



          Hope this helps!!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 19:50









          Da Mahdi03Da Mahdi03

          17117




          17117













          • Thanks that worked perfectly!!

            – FunctionalHippie
            Jan 1 at 23:26











          • @FunctionalHippie No problem!

            – Da Mahdi03
            Jan 2 at 2:30



















          • Thanks that worked perfectly!!

            – FunctionalHippie
            Jan 1 at 23:26











          • @FunctionalHippie No problem!

            – Da Mahdi03
            Jan 2 at 2:30

















          Thanks that worked perfectly!!

          – FunctionalHippie
          Jan 1 at 23:26





          Thanks that worked perfectly!!

          – FunctionalHippie
          Jan 1 at 23:26













          @FunctionalHippie No problem!

          – Da Mahdi03
          Jan 2 at 2:30





          @FunctionalHippie No problem!

          – Da Mahdi03
          Jan 2 at 2:30













          0














          Since there is only one wanted option, instead of checkbox I would use radio buttons:



          HTML Markup:



          <style>label > span {display: none;} /* hide notes by default */</style>
          <label for="replace">Replace
          <input type="radio" name="radio" id="replace" onchange="radio()" />
          <span class="note">"Replace" option is checked</span>
          </label>
          <label for="repair">Repair
          <input type="radio" name="radio" id="repair" onchange="radio()" />
          <span class="note">"Repair" option is checked</span>
          </label>


          javaScript:



          <script>
          function radio() {
          var myRadio = document.querySelectorAll('input[name="radio"]'); // get radio
          var myNotes = document.querySelectorAll('.note'); // get notes
          if (myRadio[0].checked === true) { // if the first radio checked
          myNotes[0].style.display = 'inline-block'; // show first note
          myNotes[1].style.display = 'none'; // hide the second
          }
          else { // if not do the apposite
          myNotes[0].style.display = 'none';
          myNotes[1].style.display = 'inline-block';
          }
          }
          </script>


          Hope it help you!






          share|improve this answer


























          • I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

            – FunctionalHippie
            Jan 1 at 23:16













          • Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

            – A. Meshu
            Jan 2 at 22:41






          • 1





            Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

            – FunctionalHippie
            Jan 3 at 1:21
















          0














          Since there is only one wanted option, instead of checkbox I would use radio buttons:



          HTML Markup:



          <style>label > span {display: none;} /* hide notes by default */</style>
          <label for="replace">Replace
          <input type="radio" name="radio" id="replace" onchange="radio()" />
          <span class="note">"Replace" option is checked</span>
          </label>
          <label for="repair">Repair
          <input type="radio" name="radio" id="repair" onchange="radio()" />
          <span class="note">"Repair" option is checked</span>
          </label>


          javaScript:



          <script>
          function radio() {
          var myRadio = document.querySelectorAll('input[name="radio"]'); // get radio
          var myNotes = document.querySelectorAll('.note'); // get notes
          if (myRadio[0].checked === true) { // if the first radio checked
          myNotes[0].style.display = 'inline-block'; // show first note
          myNotes[1].style.display = 'none'; // hide the second
          }
          else { // if not do the apposite
          myNotes[0].style.display = 'none';
          myNotes[1].style.display = 'inline-block';
          }
          }
          </script>


          Hope it help you!






          share|improve this answer


























          • I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

            – FunctionalHippie
            Jan 1 at 23:16













          • Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

            – A. Meshu
            Jan 2 at 22:41






          • 1





            Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

            – FunctionalHippie
            Jan 3 at 1:21














          0












          0








          0







          Since there is only one wanted option, instead of checkbox I would use radio buttons:



          HTML Markup:



          <style>label > span {display: none;} /* hide notes by default */</style>
          <label for="replace">Replace
          <input type="radio" name="radio" id="replace" onchange="radio()" />
          <span class="note">"Replace" option is checked</span>
          </label>
          <label for="repair">Repair
          <input type="radio" name="radio" id="repair" onchange="radio()" />
          <span class="note">"Repair" option is checked</span>
          </label>


          javaScript:



          <script>
          function radio() {
          var myRadio = document.querySelectorAll('input[name="radio"]'); // get radio
          var myNotes = document.querySelectorAll('.note'); // get notes
          if (myRadio[0].checked === true) { // if the first radio checked
          myNotes[0].style.display = 'inline-block'; // show first note
          myNotes[1].style.display = 'none'; // hide the second
          }
          else { // if not do the apposite
          myNotes[0].style.display = 'none';
          myNotes[1].style.display = 'inline-block';
          }
          }
          </script>


          Hope it help you!






          share|improve this answer















          Since there is only one wanted option, instead of checkbox I would use radio buttons:



          HTML Markup:



          <style>label > span {display: none;} /* hide notes by default */</style>
          <label for="replace">Replace
          <input type="radio" name="radio" id="replace" onchange="radio()" />
          <span class="note">"Replace" option is checked</span>
          </label>
          <label for="repair">Repair
          <input type="radio" name="radio" id="repair" onchange="radio()" />
          <span class="note">"Repair" option is checked</span>
          </label>


          javaScript:



          <script>
          function radio() {
          var myRadio = document.querySelectorAll('input[name="radio"]'); // get radio
          var myNotes = document.querySelectorAll('.note'); // get notes
          if (myRadio[0].checked === true) { // if the first radio checked
          myNotes[0].style.display = 'inline-block'; // show first note
          myNotes[1].style.display = 'none'; // hide the second
          }
          else { // if not do the apposite
          myNotes[0].style.display = 'none';
          myNotes[1].style.display = 'inline-block';
          }
          }
          </script>


          Hope it help you!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 22:43

























          answered Jan 1 at 20:41









          A. MeshuA. Meshu

          9072719




          9072719













          • I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

            – FunctionalHippie
            Jan 1 at 23:16













          • Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

            – A. Meshu
            Jan 2 at 22:41






          • 1





            Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

            – FunctionalHippie
            Jan 3 at 1:21



















          • I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

            – FunctionalHippie
            Jan 1 at 23:16













          • Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

            – A. Meshu
            Jan 2 at 22:41






          • 1





            Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

            – FunctionalHippie
            Jan 3 at 1:21

















          I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

          – FunctionalHippie
          Jan 1 at 23:16







          I completely agree about the radio buttons! I had wanted to use radio buttons but had even more trouble making those work so I switched to checkboxes. But I have not been able to make your code work? When I copy it into code pen it doesn't work? codepen.io/stephenskrocki/pen/EGoYaB

          – FunctionalHippie
          Jan 1 at 23:16















          Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

          – A. Meshu
          Jan 2 at 22:41





          Actually i just doublecheck it and it works perfect.. look: codepen.io/AMeshu/pen/NeXEEW.

          – A. Meshu
          Jan 2 at 22:41




          1




          1





          Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

          – FunctionalHippie
          Jan 3 at 1:21





          Thanks, I checked the code today after your edit and it works perfect! I'm going to work that into a few places in my page!

          – FunctionalHippie
          Jan 3 at 1:21


















          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%2f53998378%2fshow-hide-on-checkbox-click-does-not-work-consistently%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

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules