?
Solved

how do you make a tab be "active" based on a url

Posted on 2014-07-22
4
Medium Priority
?
206 Views
Last Modified: 2014-07-22
Hi,

I have a <ul> tab that works but I would like the tab to start as "active" based on the url. So if you come to the page from ...#web or ...#press it would highlight the appropriate tab.

Here is my code:

Thanks for the help!

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>jQuery Tab</title>
<style>
.contact_nav {
text-align:left;
margin-left:5px;
margin-top:5px;
width:705px;
}
.contact_nav li{
    display: inline-block;
    text-align: left;
    list-style-type:none;
    background-color:#417DBE;
    margin: 0px 6px 5px 0px;
    padding: 10px 15px 5px 5px;
	width:105px;
	height:75px;
	color:#FFF;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
}

.contact_nav li:hover{
	background-color:#CCC;
	height:75px;
    padding: 10px 15px 5px 5px;

}
.contact_nav li:hover a {
 color: #000;
 }
.contact_nav li a{
    text-decoration: none;
    color: #FFFFFF;
    font-family:Georgia, "Times New Roman", Times, serif;
    font-size: 20px;
	line-height:1.5em;  
}

.contact_nav li.active {
background-color:#ccc;
color:#000000;
}
.contact_nav li.active a {
color:#000000;

}
.contact_nav ul li {
cursor:pointer
}
</style>
</head>

<body>
<div class="contact_nav" style="width:685px;">
	<ul style="width:685px;">
    	<li><a class="active" href="#general">General Information</a></li>
    	<li><a href="#member">Member Services</a></li>
    	<li><a href="#patient">Patient Safety</a></li>
        <li><a href="#press">Press Inquiries</a></li>
        <li><a href="#web">Website Feedback</a></li>
    </ul><!-- //Tab buttons -->
    <div class="tabDetails">
    	<div id="general" class="tabContents">
      <p>General info</p>
        </div><!-- //tab1 -->
    	<div id="member" class="tabContents">
        	
    <p>Member Info</p>
        </div><!-- //tab2 -->
    	<div id="patient" class="tabContents">
        <p>Patient Info</p>
        </div><!-- //tab3 -->
        <div id="press" class="tabContents">
         <p>Press Info</p>
        </div><!-- //tab4 -->
        <div id="web" class="tabContents">
      
            <p>Web Info</p>
        </div><!-- //tab3 -->
    </div><!-- //tab Details -->
	
</div><!-- //Tab Container -->
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$(".tabContents").hide(); // Hide all tab content divs by default
		
		
		$(".contact_nav ul li").click(function(){ //Fire the click event
			$(".contact_nav ul li").removeClass("active");
			$(this).closest("li").addClass("active"); // Remove pre-highlighted link
			var activeTab = $("a",this).attr("href"); // Catch the click link
			$(".tabContents").hide(); // hide currently visible tab content div
			$(activeTab).fadeIn(); // show the target tab content div by matching clicked link.
			return false; //prevent page scrolling on tab click
		});
	});
</script>	
</body>
</html>

Open in new window

0
Comment
Question by:smfmetro10
[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
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40213011
Add to your ready function

$("a[href='"+location.hash+"']").closest("li").addClass("active")
0
 

Author Comment

by:smfmetro10
ID: 40213041
Thanks for the reply!
I forgot to mention that I also need the corresponding div to show as well.
Is that possible?

Thanks
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40213042
$("a[href='"+location.hash+"']").closest("li").addClass("active");
$(location.hash).show();

Open in new window

0
 

Author Closing Comment

by:smfmetro10
ID: 40213050
Outstanding! Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
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