Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:10:08+00:00 2026-05-27T21:10:08+00:00

I am new to jQuery and have managed to create a tabbed interface with

  • 0

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.

I would sincerely appreciate any insight on how to do this exactly. THANK YOU so much

<body>

<div id="tabs">

<ul>
<li><a href="#1">Tab One</a></li>
<li><a href="#2">Tab Two</a></li>
<li><a href="#3">Tab Three</a></li>
<li><a href="#4">Tab Four</a></li>
</ul>

<div id="1">
<h3>Tab 1</h3>
<p>Some content</p>
</div>
<div id="2">
<h3>Tab 2</h3>
<p>Some content</p>
</div>
<div id="3">
<h3>Tab 3</h3>
<p>Some content</p>
</div>
<div id="4">
<h3>Tab 4</h3>
<p>Some content</p>
</div>

</div> <!-- end tabs -->


<script type="text/javascript">
$(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;
});
});
</script>

</body>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:10:09+00:00Added an answer on May 27, 2026 at 9:10 pm
    var $tabs = $('#tabs div');
    
    $tabs.hide();
    
    var index = Math.floor($tabs.length * Math.random());
    
    $tabs.eq(index).show();
    

    This can be condensed a bit, I have spread it out to be a bit more understandable. I select a random number between 0 and the number of tabs -1. Then use eq to filter to that numbered element.

    http://jsfiddle.net/tnP3R/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to jQuery, and I managed to create a tab interface. I
I am very new with java script and jQuery and managed to create a
We have a single page application which loads all content through ajax calls to
I am still new to JQuery and JQuery UI Theme. I have managed to
I'm new to jQuery and JavaScript I have managed to get a small snippet
I am somewhat new to jQuery and I have a problem with something I
I have application that brings response via Ajax and creates 5-20 new jQuery click
I'm new to jQuery and I want to have a input box from a
I'm new to JQuery and was wondering how can I have Changes saved! displayed
I'm kind of new to JQuery and was wondering how can I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.