javascripthtmljqueryjquery-uijquery-ui-tabs

Is there a way to hide content in JQuery before the page loads when using Tabs?


Currently working on a dummy website using Javascript as I am new to the language.

I'm operating with JQuery and using the "tabs" function it provides; however I'd like to know how to have only the navigation bar visible as if the tab was collapsed on loading up the page.

Simply put my navigation bar is comprised of a single button as so;

<a href="#content"><button class="button">Content</button></a>

With the content as:

<p id="before">Some text before the content.</p>
<div id="content">
   <p>Just some dummy text.</p>
</div>

And just in case it is of any importance, here are the script links;

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

The script used for tab functions;

$( function() {
    $( "#tabs" ).tabs({
      collapsible: true
    });
  } );

I have tried using the visibility function provided but that doesn't suit my needs as it still leaves a gap for where the element stood. I have also tried using display:none; but the js overrides it. Also the jquery hide function doesn't seem to want to work either.

What I want is for when the website loads to have the <p> within the div: content to be hidden until the button is clicked on. And when the button is clicked on I would like the content within the div to show and for the <p id="dummy"> text to disappear. And vice versa if the content were to be shown and the button is pressed again.


Solution

  • Try

      $( function() {
        $( "#tabs" ).tabs();
      } );
      
      function handleTabFirstClick () {
        $('.myHidden').removeClass('myHidden');
      }
    #tabs div.myHidden {
      display:none !important;
    }
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
     
     
    <div id="tabs">
      <ul>
        <li><a href="#tabs-1" onclick="handleTabFirstClick()">Nunc tincidunt</a></li>
        <li><a href="#tabs-2" onclick="handleTabFirstClick()">Proin dolor</a></li>
        <li><a href="#tabs-3" onclick="handleTabFirstClick()">Aenean lacinia</a></li>
      </ul>
      <div id="tabs-1" class="myHidden tabcontent">
        <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
      </div>
      <div id="tabs-2" class="myHidden tabcontent">
        <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
      </div>
      <div id="tabs-3" class="myHidden tabcontent">
        <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
        <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
      </div>
    </div>