tags:

views:

51

answers:

3

How can I have the active link highlighted when clicked but keep the home page link highlighted until another link is clicked?

I'm using PHP if that helps.

Here is my (x)HTML code.

<div id="nav">
    <ul>
        <li><a href="http://localhost/link-1/" class="active">Link 1</a></li>
        <li><a href="http://localhost/link-2/"&gt;Link 2</a></li>
        <li><a href="http://localhost/link-3/"&gt;Link 3</a></li>
        <li><a href="http://localhost/link-4/"&gt;Link 4</a></li>
    </ul>
</div>
+1  A: 

I'm not in the spot to be able to test this right now, let me know if it work for you though.

var menuArray = new Array();

$(function() {


    $('div#nav ul li').each(function(i) {
        menuArray[i] = this;
        $(this).click(function() {
            for (var x in menuArray)
                if (x == this)
                    $(x).attr('class','active');
                else
                    $(x).attr('class','inactive');
        });
    });
});

EDIT alright, I was able to test this and it's working for me. Keep in mind that this needs to be after your HTML declaration.

$("li a").each(function(i) {
        $(this).click(function() {
             $(this).attr('class','selected');
             $("li a").not(this).attr('class','notselected');
        });
});
Robert
couldn't get it to work :(
stacks
@Robert - This is a client side (jQuery) solution when stacks asked for a server side solution (PHP).
Peter Ajtai
@Peter Ajtai It really doesn't matter if gets the job done.
stacks
Pretty sure he said he was using PHP, not that it had to be PHP.
Robert
@Robert - I use FF and NoScript on new sites I visit, so I wouldn't see the highlighting done w your method, but I would see it if done with PHP. Admittedly I'm in the minority.
Peter Ajtai
Should work for you now stacks.
Robert
+1  A: 

You can do a method in php, here is an example:

<div id="nav">
    <ul>
        <li><a href="http://localhost/link-1/" <?=$activateLink($_SERVER['REQUEST_URI'],'/link-1')?>>Link 1</a></li>
        <li><a href="http://localhost/link-2/" <?=$activateLink($_SERVER['REQUEST_URI'],'/link-2')?>>Link 2</a></li>
        <li><a href="http://localhost/link-3/" <?=$activateLink($_SERVER['REQUEST_URI'],'/link-3')?>>Link 3</a></li>
        <li><a href="http://localhost/link-4/" <?=$activateLink($_SERVER['REQUEST_URI'],'/link-4')?>>Link 4</a></li>
    </ul>
</div>
<?php
function activateLink($uri,$var) 
{
    if($uri==$var) {
        return 'class="active"';
    }
}
?>

On every page print $_SERVER['REQUEST_URI'] and pass the second parameter to the method.

KoKo
couldn't get it to work :(
stacks
A: 

That will not require PHP at all, however you can build it on PHP. I will demonstrate to you how to build this in HTML/CSS.

Try this CSS/HTML in your document (before your code):

<style>
       .active {
             color:#336699;
       }

       div#nav ul li a {
             color:#121212;
       }

       div#nav ul li a:visited {
             color:#336699;
       }
</style>

That should colour all the links blue if they have been visited, and if they havent it will be dark grey. However, if you want it to only highlight one link it's best to look at using JQuery as it has a multitude of functions to only choose the one that is being selected.

Hope that helps.

WebEntrepreneur
what JQuery function should I look for?
stacks