JQuery runtime error: object expected
The JQuery script that controls my tabcontainer is giving the expected "expected" runtime error. I honestly cannot find the reason:
$(document).ready(function() {
//When page loads...
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
});
});
Is it related to the style sheet?
a source to share
I think you want to remove you var
from the selector
activeTab.fadeIn(); //Fade in the active ID content
If you get the @ error $(document).ready(function() {
, don't forget to include the jQuery script.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
a source to share
Your code works on this html page;
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
//When page loads...
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
});
});
</script>
</head>
<body>
<ul class="tabs">
<li><a href="#one">Tab One</a></li>
<li><a href="#two">Tab Two</a></li>
</ul>
<div id="one" class="tab_content">Tab One Content</div>
<div id="two" class="tab_content">Tab Two Content</div>
</body>
</html>
so there must be something different, what is the code you are showing?
a source to share
Edited as I was wrong the first time :-)
Your problem is with the following lines:
var activeTab = $(this).find("a").attr("href"); //This will return a string value
$(activeTab).fadeIn(); //Meaning this will fail
You need to change the selector for activeTab to get the .tab_content you want to show.
I am assuming the href value is contained somewhere inside .tab_content.
a source to share