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 6929069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:16:14+00:00 2026-05-27T11:16:14+00:00

Actually I am trying to do jquery tabs. I have written a code that

  • 0

Actually I am trying to do jquery tabs. I have written a code that needs rework and probably better ways to implement. I think I could use function arguments to achieve this, but I am not sure. Can somebody tell me how to achieve this in a better way. Though my code works but I think it is rudimentary. I would also like only one tab to display a background color if this is active.

http://jsfiddle.net/5nB4P/

HTML:

<div id="nav">
    <ul>
        <li><a href="#">First Tab</a></li>
        <li><a href="#">Second Tab</a></li>
        <li><a href="#">Third Tab</a></li>
    </ul>
</div>
<div id="content">
<div class="tabs first">First Div content</div>
<div class="tabs">Second Div content</div>
<div class="tabs">Third Div content</div>
</div>

Script:

$(function() {
    $("li :eq(0)").click(function() {
            $("li").css("background","none");
            $(this).css("background","red");
            $(".tabs:gt(0)").hide();
            $(".tabs:eq(0)").show();
    })
    $("li :eq(1)").click(function() {
    $("li").css("background","none");
            $(this).css("background","red");
    $(this).css("background","red")
            $(".tabs:gt(1), .tabs:lt(1)").hide();
            $(".tabs:eq(1)").show();
    })
    $("li :eq(2)").click(function() {
    $("li").css("background","none");
            $(this).css("background","red");
            $(".tabs:lt(2)").hide();
            $(".tabs:eq(2)").show();
    })
})
  • 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-27T11:16:14+00:00Added an answer on May 27, 2026 at 11:16 am

    There is a much better way to achieve this. Here you go

    $(function() {
        $("li").click(function() {
           $(this).css("background","red").siblings().css("background","none");
           $(".tabs").hide().eq($(this).index()).show();
           return false;
        });
    })
    

    Working Demo

    As @Niels mentioned for setting the background style you can have a dedicated class(active) and add/remove this class instead of setting the inline sytle.

    FYI..In the above code $(this).index() gives the position of the first element within the jQuery object relative to its sibling elements

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

Sidebar

Related Questions

Actually I am trying to learn jQuery Ajax calls to asp.Net webservices. I have
I'm trying to put together some jQuery code that will add form elements (input
I'm trying really hard to implement jQuery UI's combo box example. I have set
Actually I'm trying to implement an ontouchlistener into my android (1.5) application. therefore i
Edit--@Uri correctly pointed out that this was an abuse of annotations; trying to actually
Got a slight problem trying to have jquery UI and knockout js to cohoperate.
i'm trying to use jquery to implement a portlet/widget style interface, with 3 columns
I'm trying to create a jQuery special event that triggers when the content that
As the title says, what I am trying to do is to have jQuery
Strange question, I know. Allow me to explain. I have a JavaScript/jQuery function that

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.