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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:03:31+00:00 2026-05-27T18:03:31+00:00

I have a navigation bar coded in html & css, and I am trying

  • 0

I have a navigation bar coded in html & css, and I am trying to add selected and deselected states to each of the buttons. I am setting the state of the first button to selected in the window.onload function like this:

<script type="text/javascript">

    window.onload = function() {
        var nav = document.getElementById('textContainer');
        var navItems = nav.getElementsByTagName('a');
        var item = navItems[0];
        item.style.color = "#696969";                       
};


</script>

I then add event handlers in another function so that buttons can be selected and deselected:

<script type="text/javascript">
    var nav = document.getElementById('textContainer');
    var activeItem = null;
    var navItems = nav.getElementsByTagName('a');

    for (var i = 0; i < navItems.length; i++) {
        navItems[i].addEventListener('click', 
        function() {

            if (activeItem) {
                activeItem.style.color = "#b3b3b3"; 
            }

            this.style.color = "#696969";
            activeItem = this;

            //alert(activeItem.innerText);

        }, false);
    }
</script>

But as I have added this, the first button, set in the window.onload function, does not deselect like the rest of the buttons, you have to click on the first button and then having done that, you can select it and deselect it at any point.

Please can you tell me where I am going wrong?

Thanks in advanced!

XcodeDev.

  • 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-27T18:03:32+00:00Added an answer on May 27, 2026 at 6:03 pm

    Try initializing activeItem with the first button, before you assign the event handlers:

    var navItems = nav.getElementsByTagName('a');
    var activeItem = navItems[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do a navigation bar using css sprites. I have the image
I have a navigation bar/list that is using only HTML and CSS. The background
I have a navigation bar that has two dropdowns (as nested ul's). I'm trying
I have a navigation bar with three buttons on the right (arrows for navigation
Basically, I am building a horizontal navigation bar. I have following markup: <ui:style src=../common.css
I have a very simple HTML layout where a vertical navigation bar should sit
I have three leaves in my navigation bar on this page . Each leaf
If have a Navigation Bar and a Tab Bar in one of my views.
Imagine I have a navigation bar in my page header, with a number of
I have created a flexible navigation bar in my app that will show 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.