?
Solved

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

Posted on 2009-03-30
4
Medium Priority
?
1,354 Views
Last Modified: 2013-11-19
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
Comment
Question by:JA67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
zeroxp earned 2000 total points
ID: 24024700
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
 

Author Comment

by:JA67
ID: 24024961
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
 
LVL 5

Expert Comment

by:zeroxp
ID: 24025075
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24025935
Great you found a solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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