Display Tab Content Immediately On Page Load












0















I am creating a cinema website (school assignment) with three tabs: "Now Showing", "Coming Soon", "Promotions". I would like all content under "Now Showing" to be immediately visible upon page load. What do I need to edit in the HTML + Javascript code to do this?



I have only learnt HTML & CSS so far and adapted the Javascript code from w3schools. Hence, I'm not sure what to do. Thank you.



Here is the HTML code.



<div id="tab-rows">

<div class="tab">
<button class="tablinks" onclick="openContent(event, 'now-
showing')">Now Showing</button>
<button class="tablinks" onclick="openContent(event, 'coming-
soon')">Coming Soon</button>
<button class="tablinks" onclick="openContent(event,
'promotions')">Promotions</button>
</div>

<div id="now-showing" class="tabcontent">
<p>Placeholder Content</p>
</div>
<div id="coming-soon" class="tabcontent">
<p>Placeholder Content</p>
</div>
<div id="promotions" class="tabcontent">
<p>Placeholder Content</p>
</div>


Here is the Javascript code.



<script>
function openContent(evt, contentName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace("
active", "");
}
document.getElementById(contentName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>









share|improve this question



























    0















    I am creating a cinema website (school assignment) with three tabs: "Now Showing", "Coming Soon", "Promotions". I would like all content under "Now Showing" to be immediately visible upon page load. What do I need to edit in the HTML + Javascript code to do this?



    I have only learnt HTML & CSS so far and adapted the Javascript code from w3schools. Hence, I'm not sure what to do. Thank you.



    Here is the HTML code.



    <div id="tab-rows">

    <div class="tab">
    <button class="tablinks" onclick="openContent(event, 'now-
    showing')">Now Showing</button>
    <button class="tablinks" onclick="openContent(event, 'coming-
    soon')">Coming Soon</button>
    <button class="tablinks" onclick="openContent(event,
    'promotions')">Promotions</button>
    </div>

    <div id="now-showing" class="tabcontent">
    <p>Placeholder Content</p>
    </div>
    <div id="coming-soon" class="tabcontent">
    <p>Placeholder Content</p>
    </div>
    <div id="promotions" class="tabcontent">
    <p>Placeholder Content</p>
    </div>


    Here is the Javascript code.



    <script>
    function openContent(evt, contentName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace("
    active", "");
    }
    document.getElementById(contentName).style.display = "block";
    evt.currentTarget.className += " active";
    }
    </script>









    share|improve this question

























      0












      0








      0








      I am creating a cinema website (school assignment) with three tabs: "Now Showing", "Coming Soon", "Promotions". I would like all content under "Now Showing" to be immediately visible upon page load. What do I need to edit in the HTML + Javascript code to do this?



      I have only learnt HTML & CSS so far and adapted the Javascript code from w3schools. Hence, I'm not sure what to do. Thank you.



      Here is the HTML code.



      <div id="tab-rows">

      <div class="tab">
      <button class="tablinks" onclick="openContent(event, 'now-
      showing')">Now Showing</button>
      <button class="tablinks" onclick="openContent(event, 'coming-
      soon')">Coming Soon</button>
      <button class="tablinks" onclick="openContent(event,
      'promotions')">Promotions</button>
      </div>

      <div id="now-showing" class="tabcontent">
      <p>Placeholder Content</p>
      </div>
      <div id="coming-soon" class="tabcontent">
      <p>Placeholder Content</p>
      </div>
      <div id="promotions" class="tabcontent">
      <p>Placeholder Content</p>
      </div>


      Here is the Javascript code.



      <script>
      function openContent(evt, contentName) {
      var i, tabcontent, tablinks;
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablinks");
      for (i = 0; i < tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace("
      active", "");
      }
      document.getElementById(contentName).style.display = "block";
      evt.currentTarget.className += " active";
      }
      </script>









      share|improve this question














      I am creating a cinema website (school assignment) with three tabs: "Now Showing", "Coming Soon", "Promotions". I would like all content under "Now Showing" to be immediately visible upon page load. What do I need to edit in the HTML + Javascript code to do this?



      I have only learnt HTML & CSS so far and adapted the Javascript code from w3schools. Hence, I'm not sure what to do. Thank you.



      Here is the HTML code.



      <div id="tab-rows">

      <div class="tab">
      <button class="tablinks" onclick="openContent(event, 'now-
      showing')">Now Showing</button>
      <button class="tablinks" onclick="openContent(event, 'coming-
      soon')">Coming Soon</button>
      <button class="tablinks" onclick="openContent(event,
      'promotions')">Promotions</button>
      </div>

      <div id="now-showing" class="tabcontent">
      <p>Placeholder Content</p>
      </div>
      <div id="coming-soon" class="tabcontent">
      <p>Placeholder Content</p>
      </div>
      <div id="promotions" class="tabcontent">
      <p>Placeholder Content</p>
      </div>


      Here is the Javascript code.



      <script>
      function openContent(evt, contentName) {
      var i, tabcontent, tablinks;
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablinks");
      for (i = 0; i < tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace("
      active", "");
      }
      document.getElementById(contentName).style.display = "block";
      evt.currentTarget.className += " active";
      }
      </script>






      javascript html






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 2:01









      sparklesparkle

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          0














          There are multiple ways of doing it.




          1. You could use the onload in the html and call your openContent function in it.

          2. If you would wish to do it with the javascript only you can add event listener have to pass in your openContent as the second argument document.addEventListener("DOMContentLoaded", openContent);






          share|improve this answer
























          • Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

            – sparkle
            Jan 1 at 8:52





















          0














          You could change:



          <div id="now-showing" class="tabcontent">


          to:



          <div id="now-showing" class="tabcontent active">


          and add this to <head>:



          <style>
          .tabcontent { display: none; }
          .tabcontent.active { display: block; }
          .tablinks.active { cursor: not-allowed; }
          </style>


          and update your script like:



          function openContent(evt, contentName) {

          var tabcontent = document.getElementsByClassName("tabcontent");
          for (var i = 0; i < tabcontent.length; i++) {
          tabcontent[i].classList.remove("active");
          }

          var tablinks = document.getElementsByClassName("tablinks");
          for (i = 0; i < tablinks.length; i++) {
          tablinks[i].classList.remove("active");
          }

          evt.currentTarget.classList.add("active");
          document.getElementById(contentName).classList.add("active");
          }


          https://codepen.io/anon/pen/qLVQmp






          share|improve this answer
























          • Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

            – sparkle
            Jan 1 at 8:51













          • Glad to be of aid. I updated the codepen link with those new requirements.

            – ram1
            Jan 1 at 9:05











          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%2f53992609%2fdisplay-tab-content-immediately-on-page-load%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














          There are multiple ways of doing it.




          1. You could use the onload in the html and call your openContent function in it.

          2. If you would wish to do it with the javascript only you can add event listener have to pass in your openContent as the second argument document.addEventListener("DOMContentLoaded", openContent);






          share|improve this answer
























          • Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

            – sparkle
            Jan 1 at 8:52


















          0














          There are multiple ways of doing it.




          1. You could use the onload in the html and call your openContent function in it.

          2. If you would wish to do it with the javascript only you can add event listener have to pass in your openContent as the second argument document.addEventListener("DOMContentLoaded", openContent);






          share|improve this answer
























          • Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

            – sparkle
            Jan 1 at 8:52
















          0












          0








          0







          There are multiple ways of doing it.




          1. You could use the onload in the html and call your openContent function in it.

          2. If you would wish to do it with the javascript only you can add event listener have to pass in your openContent as the second argument document.addEventListener("DOMContentLoaded", openContent);






          share|improve this answer













          There are multiple ways of doing it.




          1. You could use the onload in the html and call your openContent function in it.

          2. If you would wish to do it with the javascript only you can add event listener have to pass in your openContent as the second argument document.addEventListener("DOMContentLoaded", openContent);







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 3:00









          Mounik ReddyMounik Reddy

          364




          364













          • Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

            – sparkle
            Jan 1 at 8:52





















          • Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

            – sparkle
            Jan 1 at 8:52



















          Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

          – sparkle
          Jan 1 at 8:52







          Thank you! However, I am not familiar with JS at all so am unable to implement your solution successfully. Will try down the road once I learn it in my course :)

          – sparkle
          Jan 1 at 8:52















          0














          You could change:



          <div id="now-showing" class="tabcontent">


          to:



          <div id="now-showing" class="tabcontent active">


          and add this to <head>:



          <style>
          .tabcontent { display: none; }
          .tabcontent.active { display: block; }
          .tablinks.active { cursor: not-allowed; }
          </style>


          and update your script like:



          function openContent(evt, contentName) {

          var tabcontent = document.getElementsByClassName("tabcontent");
          for (var i = 0; i < tabcontent.length; i++) {
          tabcontent[i].classList.remove("active");
          }

          var tablinks = document.getElementsByClassName("tablinks");
          for (i = 0; i < tablinks.length; i++) {
          tablinks[i].classList.remove("active");
          }

          evt.currentTarget.classList.add("active");
          document.getElementById(contentName).classList.add("active");
          }


          https://codepen.io/anon/pen/qLVQmp






          share|improve this answer
























          • Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

            – sparkle
            Jan 1 at 8:51













          • Glad to be of aid. I updated the codepen link with those new requirements.

            – ram1
            Jan 1 at 9:05
















          0














          You could change:



          <div id="now-showing" class="tabcontent">


          to:



          <div id="now-showing" class="tabcontent active">


          and add this to <head>:



          <style>
          .tabcontent { display: none; }
          .tabcontent.active { display: block; }
          .tablinks.active { cursor: not-allowed; }
          </style>


          and update your script like:



          function openContent(evt, contentName) {

          var tabcontent = document.getElementsByClassName("tabcontent");
          for (var i = 0; i < tabcontent.length; i++) {
          tabcontent[i].classList.remove("active");
          }

          var tablinks = document.getElementsByClassName("tablinks");
          for (i = 0; i < tablinks.length; i++) {
          tablinks[i].classList.remove("active");
          }

          evt.currentTarget.classList.add("active");
          document.getElementById(contentName).classList.add("active");
          }


          https://codepen.io/anon/pen/qLVQmp






          share|improve this answer
























          • Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

            – sparkle
            Jan 1 at 8:51













          • Glad to be of aid. I updated the codepen link with those new requirements.

            – ram1
            Jan 1 at 9:05














          0












          0








          0







          You could change:



          <div id="now-showing" class="tabcontent">


          to:



          <div id="now-showing" class="tabcontent active">


          and add this to <head>:



          <style>
          .tabcontent { display: none; }
          .tabcontent.active { display: block; }
          .tablinks.active { cursor: not-allowed; }
          </style>


          and update your script like:



          function openContent(evt, contentName) {

          var tabcontent = document.getElementsByClassName("tabcontent");
          for (var i = 0; i < tabcontent.length; i++) {
          tabcontent[i].classList.remove("active");
          }

          var tablinks = document.getElementsByClassName("tablinks");
          for (i = 0; i < tablinks.length; i++) {
          tablinks[i].classList.remove("active");
          }

          evt.currentTarget.classList.add("active");
          document.getElementById(contentName).classList.add("active");
          }


          https://codepen.io/anon/pen/qLVQmp






          share|improve this answer













          You could change:



          <div id="now-showing" class="tabcontent">


          to:



          <div id="now-showing" class="tabcontent active">


          and add this to <head>:



          <style>
          .tabcontent { display: none; }
          .tabcontent.active { display: block; }
          .tablinks.active { cursor: not-allowed; }
          </style>


          and update your script like:



          function openContent(evt, contentName) {

          var tabcontent = document.getElementsByClassName("tabcontent");
          for (var i = 0; i < tabcontent.length; i++) {
          tabcontent[i].classList.remove("active");
          }

          var tablinks = document.getElementsByClassName("tablinks");
          for (i = 0; i < tablinks.length; i++) {
          tablinks[i].classList.remove("active");
          }

          evt.currentTarget.classList.add("active");
          document.getElementById(contentName).classList.add("active");
          }


          https://codepen.io/anon/pen/qLVQmp







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 3:26









          ram1ram1

          3,10953138




          3,10953138













          • Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

            – sparkle
            Jan 1 at 8:51













          • Glad to be of aid. I updated the codepen link with those new requirements.

            – ram1
            Jan 1 at 9:05



















          • Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

            – sparkle
            Jan 1 at 8:51













          • Glad to be of aid. I updated the codepen link with those new requirements.

            – ram1
            Jan 1 at 9:05

















          Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

          – sparkle
          Jan 1 at 8:51







          Thank you and fantastic! It works! If you don't mind helping me out with one more question: how do I make the "Now Showing" button a different colour when the page loads? At the moment, all buttons are black; if you hover or click on them it will turn grey. I would like the grey colour for "Now Showing" to appear upon loading. The CSS code for an active button is as below in my script. Thank you so much! .tab button.active { background-color: #5F5B5B; }

          – sparkle
          Jan 1 at 8:51















          Glad to be of aid. I updated the codepen link with those new requirements.

          – ram1
          Jan 1 at 9:05





          Glad to be of aid. I updated the codepen link with those new requirements.

          – ram1
          Jan 1 at 9:05


















          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%2f53992609%2fdisplay-tab-content-immediately-on-page-load%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas