Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Random Active Tab?
    primarykey
    data
    text
    <p>I am new to jQuery and have managed to create a tabbed interface with 4 sections. When the page loads, the tab content of section 1 shows up and the user can click on the others links to show other sections. In other words, tab 1 is always the first to be "active." However, I want to have a random "active" tab every time the page loads, so that other sections can be "active" as well randomly. Once the page loads with the random active tab, then the user can click to look at other sections.</p> <p>I would sincerely appreciate any insight on how to do this exactly. THANK YOU so much</p> <pre><code>&lt;body&gt; &lt;div id="tabs"&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#1"&gt;Tab One&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#2"&gt;Tab Two&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#3"&gt;Tab Three&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#4"&gt;Tab Four&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;div id="1"&gt; &lt;h3&gt;Tab 1&lt;/h3&gt; &lt;p&gt;Some content&lt;/p&gt; &lt;/div&gt; &lt;div id="2"&gt; &lt;h3&gt;Tab 2&lt;/h3&gt; &lt;p&gt;Some content&lt;/p&gt; &lt;/div&gt; &lt;div id="3"&gt; &lt;h3&gt;Tab 3&lt;/h3&gt; &lt;p&gt;Some content&lt;/p&gt; &lt;/div&gt; &lt;div id="4"&gt; &lt;h3&gt;Tab 4&lt;/h3&gt; &lt;p&gt;Some content&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- end tabs --&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $('#tabs div').hide(); // Hide all divs $('#tabs div:first').show(); // Show the first div $('#tabs ul li:first').addClass('active'); // Set the class of the first link to active $('#tabs ul li a').click(function(){ //When any link is clicked $('#tabs ul li').removeClass('active'); // Remove active class from all links $(this).parent().addClass('active'); //Set clicked link class to active var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link $('#tabs div').hide(); // Hide all divs $(currentTab).show(); // Show div with id equal to variable currentTab return false; }); }); &lt;/script&gt; &lt;/body&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload