Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1361
  • Last Modified:

How to change <li> to <li class="nav_selected"> using Onclick event of link.

I'm trying to get the Main Nav bar from the O'reilly Book's web site (http://my.safaribooksonline.com/) to work separately from their web site, and I'm having trouble understanding how in the world they change the class on the selected <li> to <li class="nav_selected">.

I have found and separated out all of the HTML and CSS that makes up the nav bar, and if I manually type in the <li class="nav_selected"> for a given link on the Nav Bar, it will highlight that tab, so I know that I have all the CSS that I need, but I cannot see for the life of me how they are recognizing which tab has been clicked and then how they are dynamically changing the Class of the <li> for the selected link and setting all other links to have no Class setting at all (so that only one link is highlighted).

I have pasted in the code that I am using. And if you use this code you should see the nav bar come up with one of the tabs selected and highlighted. I have the CSS in a separate file, as you can see by looking at the code.

I greatly appreciate any help you can give in showing me how to do this.

Thank you,
JA67
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <link rel="stylesheet" type="text/css" href="StyleSheet1.css">
  
</head>
<body>
 
   <div id="header">
        <table id="headerNavigation" cellspacing="0" cellpadding="0" border="0" width="100%">
            <tbody>
                <tr>
                    <td height="34" align="left" width="85%" valign="top">
                        <div id="nav" class="ormnav">
                            <ul>
                                <li><a href="">Link1</a></li>
                                <li><a href="">Link12</a></li>
                                <li><a href="">Link3</a></li>
                                <li class="nav_selected"><a href="">Link4</a></li>
                                <li><a href="">Link5</a></li>
                            </ul>
                        </td>
                        <td class="ancillinks" width="15%"> </td>
                    </tr>
                     <tr>
                            <td height="7" align="left" colspan="2"> 
                            <div class="subnavBlank"/>
                            </td>
                    </tr>
            </tbody>
        </table>
 </body>
</html>
 
********************** CSS CODE *********************
 
body
<style type="text/css">
 
#headerTable {
background:#FFFFFF none repeat scroll 0 0;
}
#headerNavigation {
margin-bottom:0;
}
div.ormnav {
color:#222222;
font-family:Verdana,sans-serif;
font-size:11px;
margin:0;
padding:0;
width:100%;
}
#header {
margin:0;
text-align:center;
width:100%;
}
#nav, #subnav, #footer {
width:100%;
}
#nav {
background:#000000 url(/static/200903-1632-oreilly/images/bg_nav.gif) repeat-x scroll 0 0;
margin:0;
padding:0;
}
#nav ul, #subnav ul {
list-style-image:none;
list-style-position:outside;
list-style-type:none;
margin:0;
overflow:auto;
padding:0;
}
#nav ul li {
display:inline-table;
float:left;
list-style-image:none;
list-style-position:outside;
list-style-type:none;
margin:0;
padding:0;
}
#nav ul li a {
border-right:1px solid #000000;
color:#FFFFFF;
display:block;
float:left;
font-size:12px;
line-height:30px;
margin:2px 0;
padding:0 1em;
text-decoration:none;
}
#nav ul li a {
float:none;
}
#nav ul li a:hover {
background:#000000 url(/static/200903-1632-oreilly/images/bg_nav_hover.gif) repeat-x scroll 0 0;
color:#BBBBBB;
}
#nav .nav_selected a, #nav .nav_selected a:hover {
background:#990000 none repeat scroll 0 0;
line-height:34px;
margin:0;
}
.subnavBlank {
background-color:#990000;
font-size:1px;
height:7px;
line-height:0;
margin:0;
padding:0;
}
.ancillinks {
background:#000000 url(/static/200903-1632-oreilly/images/bg_nav.gif) repeat-x scroll 0 0;
}
#nav li.nav_selected a, #nav li.nav_selected a:hover {
background:#990000 none repeat scroll 0 0 !important;
line-height:34px !important;
margin:0 !important;
}
 
</style>

Open in new window

0
JA67
Asked:
JA67
  • 2
1 Solution
 
zeroxpCommented:
this is a quick test, you can do more tweak as you need
<html>
<head>
<title>test</title>
<script type="text/javascript">
function select(lnk)
{
    if (lnk && lnk.parentNode && lnk.parentNode.parentNode)
    {
        for (var i = 0; i < lnk.parentNode.parentNode.childNodes.length; i ++)
        {
            lnk.parentNode.parentNode.childNodes[i].className = "";
        }
        lnk.parentNode.className = "selected";
    }
}
</script>
<style type="text/css">
.selected a {background-color:black; color:white;}
</style>
</head>
<body>
<div>
    <ul>
    <li><a href="#" onclick="select(this); return false;">item1</a></li>
    <li><a href="#" onclick="select(this); return false;">item2</a></li>
    <li><a href="#" onclick="select(this); return false;">item3</a></li>
    </ul>
</div>
</body>
</html>

Open in new window

0
 
JA67Author Commented:
I am always soooo blown away buy people like you who really know how to program in JavaScript! YOU ROCK!!!!!!!!! It works! Man alive it is just so frustrating to not "get it" the way you do. Do you have any recomendations on the best way to learn JavaScript?

Thank you VERY MUCH. I greatly appreciate your help.

JA67
0
 
zeroxpCommented:
w3schools.com is a great site for web developers.

to learn javascript Danny Goodman's JavaScript Bible is a very good book. it is always on my desk. down the way you will find a lot browser specific problems, google will always help.

0
 
Michel PlungjanIT ExpertCommented:
Great you found a solution.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now