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

  • Home
  • SEARCH
  • 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 8047141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:00:38+00:00 2026-06-05T06:00:38+00:00

Problem: I have a list that consist of two tabs. When I submit a

  • 0

Problem:

I have a list that consist of two tabs. When I submit a form and the page reloads I would like to track which tab the user was last on in order to activate it again.

HTML code:

<ul id="myTab" class="nav nav-tabs">
    <li class="active">
        <a href="#participants" data-toggle="tab">Participants</a>
    </li>
    <li>
        <a href="#gradevalues" data-toggle="tab">Criteria</a>
    </li>
</ul>

PHP code:

The PHP will generate a variable $subtabname that contains “participants” or “gradevalues”.

Scenario:

If I am on #gradevalues and submit the form, the variable $subtabname will contain “gradevalues”. I would jQuery like to remove “active” class from #participants and add it instead to the li for #gradevalues.

Question:

How would I with jQuery do to find the element using this PHP variable and move to it’s parent li to remove/add an “active” class?

[UPDATE]: Non-optimal working jQuery code

$( "#myTab li.active" ).removeClass("active");
$( "#myTab a[href=#'.$subtabname.']" ).parent("li").addClass("active");
  • 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-06-05T06:00:40+00:00Added an answer on June 5, 2026 at 6:00 am

    In the PHP file:

    <body data-activetab="<?php echo $subtabname ?>">
    

    In the jQuery file:

    (function () {
        var subtabname = $( 'body' ).data( 'activetab' );
        var $activeTab = $( 'a[href="#' + subtabname '"]', 'myTab' ).parent();
        $activeTab.addClass( 'active' ).siblings().addClass( 'active' );
    }());
    

    I’ve optimized your code a bit:

    (function ( $ul, active ) {
        $ul.children().removeClass( active );
        $ul.find( 'a[href="#.$subtabname."]' ).parent().removeClass( active );
    }( $( '#myTab' ), 'active' ));
    

    I pass the class name as a string to avoid code repetition (in your code, the class name string repeats three times). I also reduced the number of DOM queries from two to one.

    Btw, if you need the reference to the #myTab element in some other parts of your code, cache it in a variable, and use that variable instead of doing multiple DOM queries for that same element.

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

Sidebar

Related Questions

I have a page which list comments of users. My problem is that I
I'm experiencing a very curious problem. I have a list named theorems that has
My problem is simple: I have a long list of elements that I want
Here's the problem. In my app, I have 5 tabs which contains activities. In
I am creating an iPhone app which I would like to have a similar
I have a single page jQuery mobile application that consists of several list views
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I need help with CMD scripts. Here is my problem: I have list of
Problem: I have a list containg hours, for example: 08:15:00 08:45:00 09:00:00 12:00:00 ...
The problem: I have a div where I will have a list of selections

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.