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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:10:49+00:00 2026-06-12T11:10:49+00:00

I can’t seem to use the function changeTab(num) to change the ID of a

  • 0

I can’t seem to use the function changeTab(num) to change the ID of a li element from number to selected, and to revert selected tab’s ID to its default number. It only works one or two times and then stops. My goal is to mimic the change of selected and unselected tab, like for example in Chrome tabs.

<ul id="nav">
                    <li onClick="changeTab(1);" id="1"><a  href="#">Nav 1</a></li>
                    <li onClick="changeTab(2);" id="2"><a  href="#">Nav 2</a></li>
                    <li onClick="changeTab(3);" id="selected"><a href="#">Nav 3</a></li>
                    <li onClick="changeTab(4);" id="4"><a  href="#">Nav 4</a></li>
</ul>

My JavaScript code is:

function changeTab(num){

    switch(num){
        case 1:
            document.getElementById("selected").id = "1";
            break;
        case 2:
            document.getElementById("selected").id = "2";
            break;
        case 3:
            document.getElementById("selected").id = "3";
            break;
        case 4:
            document.getElementById("selected").id = "4";
            break;
        default:
            document.getElementById("selected").color = "";
    }


    //
    document.getElementById(num).id = "selected";
  • 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-12T11:10:50+00:00Added an answer on June 12, 2026 at 11:10 am

    EDIT as WTK suggested (as a comment in your question above) for this to be valid HTML, id values must start with a letter and not a number… I’ve updated my answer to be valid HTML by prepending the id with nav-…

    <ul id="nav">
        <li onclick="changeTab(this);" id="nav-1"><a  href="#">Nav 1</a></li>
        <li onclick="changeTab(this);" id="nav-2"><a  href="#">Nav 2</a></li>
        <li onclick="changeTab(this);" id="selected"><a href="#">Nav 3</a></li>
        <li onclick="changeTab(this);" id="nav-4"><a  href="#">Nav 4</a></li>
    </ul>
    

    Using the this variable within the onclick handler will get the element being clicked… Then you can use the following function as the handler…

    function changeTab(el) {
      // This function is passed 'el' from the onclick handler of the li. The
      // onclick handler passes 'this' through as the 'el' argument. 'el' will 
      // be a HTMLElement object. 
    
      // We only want to do something if the 'el' HTMLElement object does not
      // currently have the 'id' "selected", otherwise we do nothing.
      if(el.id != "selected") {
        // Revert all tabs to their original ids
    
        // Try and find the HTMLElement with the id "nav". The variable 'nav'
        // will be another HTMLElement object, this time representing the ul element.
        var nav = document.getElementById("nav");
    
        // The function 'getElementsByTagName' always returns a 
        // HTMLElementCollection, it might have zero elements if there were no
        // matches. We can use it as an array (although there are things to
        // take into consideration that affect performance). The 
        // HTMLElementCollection will contain all li elements that are 
        // descendants of the 'nav' ul element
        var lis = nav.getElementsByTagName("li");
    
        // Here we do a for-loop to iterate through the element collection
        // each item in the HTMLElementCollection will be a HTMLElement
        // representing one of the li elements
        for(var i = 0; i < lis.length; ++i) { // Arrays are zero-indexed
    
          // We set the id to nav-n overwriting whatever was there previously
          lis[i].id = "nav-" + (i + 1); // Our tabs are one-indexed
        }
    
        // Set the id for the original HTMLElement that was passed into the
        // function to "selected", we do this step last as one of the li HTMLElements
        // we change in the for-loop above will also be this HTMLElement
        el.id = "selected";
      }
    }
    

    There are other, possibly better, ways to do this. This should solve the problem though, if you wanted to delve deeper I would recommend the book Pro JavaScript Design Patterns.

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

Sidebar

Related Questions

Can we change the HTML or its attributes of the selected part of the
Can we change the default action of the edit selected row button? Here is
Can anyone let me know how can we change the value of kendo combobox
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can I be sure about the order in a Python dictionary? The function op.GetTangent(id)
Can someone please explain to me why the output from the following code is
Can anybody tell me a regular expression to use within some PHP to find
Can I continue to use the interbase vcl components in delphi 2010 with firebird
Can MOSS integrate and get user profiles from multiple Active Directory and/or LDAP stores?
Can a Visual Studio 2008 custom tool be passed additional parameters from the custom

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.