Selecting specific tab on page load

4 replies·opened Dec 15, 2017
I
icefieldDec 15, 2017

Having difficulty showing specific tab on a page I'm developing. To demonstrate issue, I've slimmed down your tabs.html to the following, and added a bit of JS at the bottom to attempt setting a specific tab on page load. Can't get any tab but the first to show. Could you provide some guidance? Thanks.

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>

	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="author" content="SemiColonWeb" />

	<!-- Stylesheets
	============================================= -->
	<link href="http://fonts.googleapis.com/css?family=Lato:300,400,400italic,600,700|Raleway:300,400,500,600,700|Crete+Round:400italic" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
	<link rel="stylesheet" href="style.css" type="text/css" />
	<link rel="stylesheet" href="css/dark.css" type="text/css" />
	<link rel="stylesheet" href="css/font-icons.css" type="text/css" />
	<link rel="stylesheet" href="css/animate.css" type="text/css" />
	<link rel="stylesheet" href="css/magnific-popup.css" type="text/css" />

	<link rel="stylesheet" href="css/responsive.css" type="text/css" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />

	<!-- Document Title
	============================================= -->
	<title>Tabs | Canvas</title>

</head>

<body class="stretched">

	<!-- Document Wrapper
	============================================= -->
	<div id="wrapper" class="clearfix">

		<!-- Content
		============================================= -->
		<section id="content">

			<div class="content-wrap">

				<div class="container clearfix">

					<!-- Post Content
					============================================= -->
					<div class="postcontent nobottommargin clearfix">

						<div class="tabs clearfix" id="tab-1">

							<ul class="tab-nav clearfix" id="ul-tab-1">
								<li><a href="#tabs-1"><i class="icon-home2 norightmargin"></i></a></li>
								<li><a href="#tabs-2">Nunc tincidunt</a></li>
								<li><a href="#tabs-3">Proin dolor</a></li>
								<li class="hidden-xs"><a href="#tabs-4">Aenean lacinia</a></li>
							</ul>

							<div class="tab-container">

								<div class="tab-content clearfix" id="tabs-1">
									Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.
								</div>
								<div class="tab-content clearfix" id="tabs-2">
									Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.
								</div>
								<div class="tab-content clearfix" id="tabs-3">
									<p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
									Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.
								</div>
								<div class="tab-content clearfix" id="tabs-4">
									Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.
								</div>

							</div>

						</div>
					</div><!-- .sidebar end -->

				</div>

			</div>

		</section><!-- #content end -->

	</div><!-- #wrapper end -->

	<!-- Go To Top
	============================================= -->
	<div id="gotoTop" class="icon-angle-up"></div>

	<!-- External JavaScripts
	============================================= -->
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/plugins.js"></script>

	<!-- Footer Scripts
	============================================= -->
	<script type="text/javascript" src="js/functions.js"></script>

	<script>
		jQuery(document).ready( function($){
			$('[href="#tabs-3"]').tab('show');				// By name (without parent)
//			$('#ul-tab-1 [href="#tabs-3"]').tab('show');	// By name (with parent)
//			$('#tab-1 [href="#tabs-3"]').tab('show');		// By name (with ancester)
// 			$('#tab-1 a:first').tab('show');				// Select first tab
// 			$('#tab-1 a:last').tab('show');					// Select last tab
// 			$('#tab-1 li:eq(2) a').tab('show');				// Select third tab (0-indexed)
		});
	</script>
</body>
</html>
S
SemiColonWebSTAFFDec 15, 2017

Hello,

This functionality is already included within Canvas. Simply consider adding the data-active="2" Attribute to the .tabs Element to activate the 2nd Tab on Load. Example code:

<div class="tabs clearfix" data-active="2">

	<ul class="tab-nav clearfix">
		<li><a href="#tab-1">Tab 1</a></li>
		<li><a href="#tab-2">Tab 2</a></li>
		<li><a href="#tab-3">Tab 3</a></li>
	</ul>

	<div class="tab-container">

		<div class="tab-content clearfix" id="tab-1">
			Tab Content 1
		</div>
		<div class="tab-content clearfix" id="tab-2">
			Tab Content 2
		</div>
		<div class="tab-content clearfix" id="tab-3">
			Tab Content 3
		</div>

	</div>

</div>

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

I
icefieldDec 15, 2017

So foobar.tab('show') is specific to bootstrap tabs.

Using foobar.click() on Canvas tabs works as I need it.

Hope this helps someone in the future.

I
icefieldDec 15, 2017

Want to set the tab dynamically depending on some other PHP//JS values. Using the info in the next message in this thread, I was able to get the desired tab displaying on page reload using the foobar.click() technique.

S
SemiColonWebSTAFFDec 15, 2017

Hello,

This is Possible too! Add the id="tabs-1" Attribute to the .tabs Element and then add the following JS Code at the bottom of the Page just after the js/functions.js JS File Linking:

<script>
	jQuery(window).on( 'load', function(){
		jQuery( '#tabs-1' ).tabs( 'option', 'active', 1 );
	});
</script>

Remember that the value 1 here represents the index value of the Tab starting from 0. So the 1 value activates the 2nd Tab.

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard