button needs to be clicked twice to make trigger the script












1















I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.



Thanks for the help.






#div1{width: 400px; background-color: beige; transition: 1s}
.div2{background-color: aquamarine}
.div2 button{float: right; width: 50px;cursor: pointer; outline: none}
.div2 p{height: 0px;overflow: hidden;transition: 1s}
.div3 img{padding: 5%}

<div id="div1">
<div class="div2">
<h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
<p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
<script>
function showtext(){
var but = document.getElementById("buttonst");
var p = document.getElementById("ptext");
var div = document.getElementById("div1");
if (p.style.height === "0px"){
but.innerHTML = "v";
p.style.height = "200px";
div.style.backgroundColor = "aquamarine";
} else {
but.innerHTML = "+";
p.style.height = "0px";
div.style.backgroundColor = "beige";
}
}
</script>
</div>
<div class="div3">
<img src="imagefile" alt="" width="90%">
</div>
</div>












share|improve this question





























    1















    I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.



    Thanks for the help.






    #div1{width: 400px; background-color: beige; transition: 1s}
    .div2{background-color: aquamarine}
    .div2 button{float: right; width: 50px;cursor: pointer; outline: none}
    .div2 p{height: 0px;overflow: hidden;transition: 1s}
    .div3 img{padding: 5%}

    <div id="div1">
    <div class="div2">
    <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
    <p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
    <script>
    function showtext(){
    var but = document.getElementById("buttonst");
    var p = document.getElementById("ptext");
    var div = document.getElementById("div1");
    if (p.style.height === "0px"){
    but.innerHTML = "v";
    p.style.height = "200px";
    div.style.backgroundColor = "aquamarine";
    } else {
    but.innerHTML = "+";
    p.style.height = "0px";
    div.style.backgroundColor = "beige";
    }
    }
    </script>
    </div>
    <div class="div3">
    <img src="imagefile" alt="" width="90%">
    </div>
    </div>












    share|improve this question



























      1












      1








      1








      I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.



      Thanks for the help.






      #div1{width: 400px; background-color: beige; transition: 1s}
      .div2{background-color: aquamarine}
      .div2 button{float: right; width: 50px;cursor: pointer; outline: none}
      .div2 p{height: 0px;overflow: hidden;transition: 1s}
      .div3 img{padding: 5%}

      <div id="div1">
      <div class="div2">
      <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
      <p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
      <script>
      function showtext(){
      var but = document.getElementById("buttonst");
      var p = document.getElementById("ptext");
      var div = document.getElementById("div1");
      if (p.style.height === "0px"){
      but.innerHTML = "v";
      p.style.height = "200px";
      div.style.backgroundColor = "aquamarine";
      } else {
      but.innerHTML = "+";
      p.style.height = "0px";
      div.style.backgroundColor = "beige";
      }
      }
      </script>
      </div>
      <div class="div3">
      <img src="imagefile" alt="" width="90%">
      </div>
      </div>












      share|improve this question
















      I have the following problem: I have to click on the button twice to make the script work, and I don't know why. I've tried to read some posts with the search but none of them have resolved my question.



      Thanks for the help.






      #div1{width: 400px; background-color: beige; transition: 1s}
      .div2{background-color: aquamarine}
      .div2 button{float: right; width: 50px;cursor: pointer; outline: none}
      .div2 p{height: 0px;overflow: hidden;transition: 1s}
      .div3 img{padding: 5%}

      <div id="div1">
      <div class="div2">
      <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
      <p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
      <script>
      function showtext(){
      var but = document.getElementById("buttonst");
      var p = document.getElementById("ptext");
      var div = document.getElementById("div1");
      if (p.style.height === "0px"){
      but.innerHTML = "v";
      p.style.height = "200px";
      div.style.backgroundColor = "aquamarine";
      } else {
      but.innerHTML = "+";
      p.style.height = "0px";
      div.style.backgroundColor = "beige";
      }
      }
      </script>
      </div>
      <div class="div3">
      <img src="imagefile" alt="" width="90%">
      </div>
      </div>








      #div1{width: 400px; background-color: beige; transition: 1s}
      .div2{background-color: aquamarine}
      .div2 button{float: right; width: 50px;cursor: pointer; outline: none}
      .div2 p{height: 0px;overflow: hidden;transition: 1s}
      .div3 img{padding: 5%}

      <div id="div1">
      <div class="div2">
      <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
      <p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
      <script>
      function showtext(){
      var but = document.getElementById("buttonst");
      var p = document.getElementById("ptext");
      var div = document.getElementById("div1");
      if (p.style.height === "0px"){
      but.innerHTML = "v";
      p.style.height = "200px";
      div.style.backgroundColor = "aquamarine";
      } else {
      but.innerHTML = "+";
      p.style.height = "0px";
      div.style.backgroundColor = "beige";
      }
      }
      </script>
      </div>
      <div class="div3">
      <img src="imagefile" alt="" width="90%">
      </div>
      </div>





      #div1{width: 400px; background-color: beige; transition: 1s}
      .div2{background-color: aquamarine}
      .div2 button{float: right; width: 50px;cursor: pointer; outline: none}
      .div2 p{height: 0px;overflow: hidden;transition: 1s}
      .div3 img{padding: 5%}

      <div id="div1">
      <div class="div2">
      <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
      <p id="ptext">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
      <script>
      function showtext(){
      var but = document.getElementById("buttonst");
      var p = document.getElementById("ptext");
      var div = document.getElementById("div1");
      if (p.style.height === "0px"){
      but.innerHTML = "v";
      p.style.height = "200px";
      div.style.backgroundColor = "aquamarine";
      } else {
      but.innerHTML = "+";
      p.style.height = "0px";
      div.style.backgroundColor = "beige";
      }
      }
      </script>
      </div>
      <div class="div3">
      <img src="imagefile" alt="" width="90%">
      </div>
      </div>






      javascript html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 3:06









      Juan

      65112




      65112










      asked Nov 22 '18 at 21:25









      MattiaMattia

      43




      43
























          2 Answers
          2






          active

          oldest

          votes


















          1














          On first time it goes inside the else block of your function.I solved adding style="height:0" on the


          tag.



          <div id="div1">
          <div class="div2">
          <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
          <p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
          lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
          <script>
          function showtext(){

          var but = document.getElementById("buttonst");
          var p = document.getElementById("ptext");
          var div = document.getElementById("div1");
          if (p.style.height === "0px"){

          but.innerHTML = "v";
          p.style.height = "200px";
          div.style.backgroundColor = "aquamarine";
          } else {

          but.innerHTML = "+";
          p.style.height = "0px";
          div.style.backgroundColor = "beige";
          }
          }
          </script>
          </div>
          <div class="div3">
          <img src="imagefile" alt="" width="90%">
          </div>
          </div>





          share|improve this answer

































            0














            Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.



            As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.






            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%2f53438135%2fbutton-needs-to-be-clicked-twice-to-make-trigger-the-script%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









              1














              On first time it goes inside the else block of your function.I solved adding style="height:0" on the


              tag.



              <div id="div1">
              <div class="div2">
              <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
              <p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
              lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
              <script>
              function showtext(){

              var but = document.getElementById("buttonst");
              var p = document.getElementById("ptext");
              var div = document.getElementById("div1");
              if (p.style.height === "0px"){

              but.innerHTML = "v";
              p.style.height = "200px";
              div.style.backgroundColor = "aquamarine";
              } else {

              but.innerHTML = "+";
              p.style.height = "0px";
              div.style.backgroundColor = "beige";
              }
              }
              </script>
              </div>
              <div class="div3">
              <img src="imagefile" alt="" width="90%">
              </div>
              </div>





              share|improve this answer






























                1














                On first time it goes inside the else block of your function.I solved adding style="height:0" on the


                tag.



                <div id="div1">
                <div class="div2">
                <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
                <p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
                lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
                <script>
                function showtext(){

                var but = document.getElementById("buttonst");
                var p = document.getElementById("ptext");
                var div = document.getElementById("div1");
                if (p.style.height === "0px"){

                but.innerHTML = "v";
                p.style.height = "200px";
                div.style.backgroundColor = "aquamarine";
                } else {

                but.innerHTML = "+";
                p.style.height = "0px";
                div.style.backgroundColor = "beige";
                }
                }
                </script>
                </div>
                <div class="div3">
                <img src="imagefile" alt="" width="90%">
                </div>
                </div>





                share|improve this answer




























                  1












                  1








                  1







                  On first time it goes inside the else block of your function.I solved adding style="height:0" on the


                  tag.



                  <div id="div1">
                  <div class="div2">
                  <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
                  <p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
                  lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
                  <script>
                  function showtext(){

                  var but = document.getElementById("buttonst");
                  var p = document.getElementById("ptext");
                  var div = document.getElementById("div1");
                  if (p.style.height === "0px"){

                  but.innerHTML = "v";
                  p.style.height = "200px";
                  div.style.backgroundColor = "aquamarine";
                  } else {

                  but.innerHTML = "+";
                  p.style.height = "0px";
                  div.style.backgroundColor = "beige";
                  }
                  }
                  </script>
                  </div>
                  <div class="div3">
                  <img src="imagefile" alt="" width="90%">
                  </div>
                  </div>





                  share|improve this answer















                  On first time it goes inside the else block of your function.I solved adding style="height:0" on the


                  tag.



                  <div id="div1">
                  <div class="div2">
                  <h3>prova 1<button id="buttonst" onClick="showtext()">+</button></h3>
                  <p id="ptext" style="height:0">lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum,
                  lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, lorem ipsum, </p>
                  <script>
                  function showtext(){

                  var but = document.getElementById("buttonst");
                  var p = document.getElementById("ptext");
                  var div = document.getElementById("div1");
                  if (p.style.height === "0px"){

                  but.innerHTML = "v";
                  p.style.height = "200px";
                  div.style.backgroundColor = "aquamarine";
                  } else {

                  but.innerHTML = "+";
                  p.style.height = "0px";
                  div.style.backgroundColor = "beige";
                  }
                  }
                  </script>
                  </div>
                  <div class="div3">
                  <img src="imagefile" alt="" width="90%">
                  </div>
                  </div>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 22:16

























                  answered Nov 22 '18 at 21:34









                  KurohigeKurohige

                  5901520




                  5901520

























                      0














                      Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.



                      As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.






                      share|improve this answer




























                        0














                        Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.



                        As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.






                        share|improve this answer


























                          0












                          0








                          0







                          Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.



                          As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.






                          share|improve this answer













                          Your code checks the style property for the ptext element which refers only to inline styling. Since the height is applied by a CSS class and not as inline styling.



                          As suggested by mattia you can add the height as inline styling from the get-go, or if you prefer to use CSS as you currently are, a possible solution is posed in this question.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 0:19









                          JuanJuan

                          65112




                          65112






























                              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%2f53438135%2fbutton-needs-to-be-clicked-twice-to-make-trigger-the-script%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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              Refactoring coordinates for Minecraft Pi buildings written in Python