Jump to content

[SOLVED] JavaScript List


conormacaoidh

Recommended Posts

Hi,

 

I can see how this will be both a difficult question to explain and to answer. I am dealing with the following list:

 

<ul id="list">
  <li><a href="#" id="page1">Page One</a></li>
  <li><a href="#" id="page2">Page Two</a></li>
  <li><a href="#" id="page3">Page Three</a></li>
  <li><a href="#" id="page4">Page Four</a></li>
</ul>

 

What I want to do is to write a piece of JavaScript that will detect what page is currently selected and then apply certain style rules to that page through the id in the list. This much I have achieved by creating a function and passing the pagename through the function.

 

I just want to know is there any easier way of going about such a task. Is it possible to get the current filename through JavaScript and then apply styles to links that are linked to that URL, without the use of an id?

 

Or am I off track completely? Can anyone think of a better way to go about such a task?

 

This is where it gets more complicated... If I manage to pull of that much i want to take advantage of CSS3 and set the li below the selected li to have -moz-border-radius-topright:5px; and the li above the selected li to have -moz-border-radius-bottomright:5px;

 

I know that i'm asking a lot but any help would be greatly appreciated.

Link to comment
Share on other sites

to get you started...here is something written with jQuery that will tag the LI of the current page with a class 'current':

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
    var thisUrl = window.location.href;
    if(i = thisUrl.indexOf('?')){
      thisUrl = thisUrl.substring(0,i);
    }
    $('#list li a').each(function(){
      if(this.href == thisUrl){
        $(this.parentNode).addClass('current');
      } 
    });
  });
</script>
<ul id="list">
  <li><a href="home.php" id="page1">Page One</a></li>
  <li><a href="about.php" id="page2">Page Two</a></li>
  <li><a href="test.php" id="page3">Page Three</a></li>
  <li><a href="contact.php" id="page4">Page Four</a></li>
</ul>

Link to comment
Share on other sites

my bad...

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
    var thisUrl = window.location.href;
    var i = thisUrl.indexOf('?');
    if(i >= 0){
     thisUrl = thisUrl.substring(0,i);
    }
    $('#list li a').each(function(){
      if(this.href == thisUrl){
        $(this.parentNode).addClass('current');
      }
    });
  });
</script>
<ul id="list">
  <li><a href="home.php" id="page1">Page One</a></li>
  <li><a href="about.php" id="page2">Page Two</a></li>
  <li><a href="test.php" id="page3">Page Three</a></li>
  <li><a href="contact.php" id="page4">Page Four</a></li>
</ul>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.