[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

changing link state onload

Posted on 2005-04-16
3
Medium Priority
?
756 Views
Last Modified: 2008-02-01
This nicely adds a class to the link if the href is equal to the url:

function current()
{
      var lnk = document.links;
      for(var i=0;i<lnk.length;i++)
      {
            if(lnk[i].href.indexOf(self.location.pathname)>-1) lnk[i].className = "onstate";
      }
}

window.onload = function()
{
     current();
}

However, it applies to all links on the page. Ideally, I need it to "only" apply to the following container:

<ul id="navlist">
      <li><a id="nav1" href="one.html">menu link</a><li>
      <li><a id="nav2" href="two.html">menu link</a><li>
      <li><a id="nav3" href="three.html">menu link</a><li>
</ul>

The number of li tags is variable, but perhaps accessing GetElementById for navlist would be sufficient?

Thanks,
Sean
0
Comment
Question by:seanpowell
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 total points
ID: 13798148
Here an example:

<html>
<head>
<style>
.onstate {
  color: red;
}
</style>
<script>
function current(){
    var lnk = document.getElementById('navlist').getElementsByTagName('a');
    for(var i=0;i<lnk.length;i++)
    {
         if(lnk[i].href.indexOf(self.location.pathname)>-1) lnk[i].className = "onstate";
    }
}

window.onload = current;
</script>
</head>
<body>
<ul id="navlist">
    <li><a id="nav1" href="one.html">menu link</a><li>
    <li><a id="nav2" href="two.html">menu link</a><li>
    <li><a id="nav3" href="three.html">menu link</a><li>
</ul>
<ul>
    <li><a id="nav1" href="one.html">menu link</a><li>
    <li><a id="nav2" href="two.html">menu link</a><li>
    <li><a id="nav3" href="three.html">menu link</a><li>
</ul>
</body>
</html>



0
 
LVL 31

Author Comment

by:seanpowell
ID: 13798169
That's absolutely perect - you nailed it in one.

Thanks Zvonko :-)
Sean
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 13798758
You are welcome.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question