Solved

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

Posted on 2009-03-30
4
1,342 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
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
zeroxp earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile menu navigation , its html and css 6 54
Flexbox in CSS3 2 39
Re-position sub-options beneath the TAB 7 75
How do i set padding within the sidebar? 2 27
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now