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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:52:29+00:00 2026-05-23T13:52:29+00:00

I have a list of hrefs inside UL. When a user clicks a link,

  • 0

I have a list of hrefs inside UL. When a user clicks a link, I need to apply a particular CSS style to that href (i.e. make it ‘selected’ and make all others ‘unselected’). The script I used is given below. But it does not work. Any idea where it went wrong?

Thanks

    Coaliza Anti-Cardiolipin

    Coamatic Antithrombin

    <script type="text/javascript" language="javascript"  >
        document.onclick = function(evt) {
            var el = window.event ? event.srcElement : evt.target;
            var aText = "";
            if (el && el.className == "unselected") {
                el.className = "selected";
                aText = el.innerText;

                for (var i = 0; i < document.links.length; ++i) {
                    var a = document.links[i];
                    if (a.className == "selected" && a.innerText != aText)
                        a.className = "unselected";


                }
                /*
                var siblings = el.parentNode.childNodes;
                for (var i = 0, l = siblings.length; i < l; i++) {
                var sib = siblings[i];
                if (sib != el && sib.className == "selected")
                sib.className = "unselected";
                }*/
            }
        }
</script>
  • 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-23T13:52:30+00:00Added an answer on May 23, 2026 at 1:52 pm

    Though I would like to see the HTML I would strongly suggest that you use a Javascript Framework, jQuery would give you a strong push with much less code.

    Just add the script src to your document

    and use only this code

    <script type="text/javascript">
    
        // well all the DOM is written in the page and all elements are available do:
        $(document).ready( function() {
    
            // for each <a> tag bind the event click and now do:     
            $("a").click(function() {
    
                // 1st. remove all selected
                $("a.selected").removeClass("selected");
    
                // 2nd. assign selected to this clicked element only
                $(this).addClass("selected");
    
                // 3rd. let's return true so the <a> tag can jump to the href
                return true;    
            });
        });
    
    </script>
    

    There is no need to have unselected class, that’s should be the default CSS.

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

Sidebar

Related Questions

I have a list of categories that need to live inside of the servletContext
i have list of rows that user select and i want to delete them,
I have list items that displayed inline. I want to align them vertically inside
I've coded myself into a CSS corner. I have a list of images that
I have code that looks like this: <div id="header"> <ul class="tabs"> <li><a href="/user/view"><span class="tab">Profile</span></a></li>
I have list of links, like: <div class=links> <a href=# class=link1>link 1</a> <a href=#
I have a javascript code (i) that write several <div><a href=...></a></div> inside the <div
I have a list like that: <div class=cloned><a rel=test href= title=></a></div> <div><a rel=test href=
I have a list. <ul id=navigation> <li><a href=#></a></li> <li><a href=#></a></li> <li><a href=#></a></li> <li><a href=#></a></li>
I have a simple ul list. the li's contain simple a href's. I have

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.