Link to a specific tab from the same page tab
Hello,
I need to be able to link to a specific tab from a menu that is present on all the pages. This means that the link has to activate a particular tab while on the same page (this is important, I can get it to work from another page). In the current version of jquery UI, I can link from another page directly to the tab, no problems. But not the same thing happens if the link is on the same page with the tab I am trying to access.
I have 2 questions:
1. there something I can do to make the link-to-tab on the same page behave like the link-to-tab from another page? This would be the ideal scenario. To clarify - this works just putting href="url/#tab-id" without anything else.
2. is there another solution linking by referring to the ID of the tab (or some other parameter I can put in)? I can get this to work using the position of the tab, but alas, the system I have to build has to be agnostic to the tab's position, as it will be re-used in different pages and different configurations.
- <script>
- /*link-to-tab from external page */
- $(function() {
- $('#tabs').tabs({
- select: function(event, ui) {
- window.location.hash = ui.tab.hash;
- }
- });
- });
-
- /*link-to-tab by using it's position */
- jQuery(document).ready(function($){
- $('#link-1').click(function(){
- $('#tabs').tabs( 'option', 'active', 0 ); // Selects the first tab
- });$('#link-2').click(function(){
- $('#tabs').tabs( 'option', 'active', 1 ); // Selects the first tab
- });$('#link-3').click(function(){
- $('#tabs').tabs( 'option', 'active', 2 ); // Selects the first tab
- });
- });
- </script>
The HTML
- <h2>These SHOULD link to tab ID </h2>
- <p>They are the same as on the <a href="/tabs/links.html">links.html</a> page, same code, etc. THey don't work :(</p>
- <a href="#tabs-1">Tab 1</a><br />
- <a href="http://agileprojects.ro/tabs/tabs.html#tabs-2">Tab 2</a><br />
- <a href="#tabs-3">Tab 3</a><br />
- <br /><br /><br />
-
- <h2>These link to tab position </h2>
- <p>aka, they activate tab 1, tab 2, tab 3, etc. </p>
- <a href="#tabs-1" id="link-1">Tab 1</a><br />
- <a href="#tabs-2" id="link-2">Tab 2</a><br />
- <a href="#tabs-3" id="link-3">Tab 3</a><br />
-
- <div id="tabs">
- <ul>
- <li><a href="#tabs-1">Tab 1</a></li>
- <li><a href="#tabs-2">Tab 2</a></li>
- <li><a href="#tabs-3">Tab 3</a></li>
- </ul>
- <div id="tabs-1">
- <p>some test some test</p>
- </div>
- <div id="tabs-2">
- <p>some test some test</p>
- </div>
- <div id="tabs-3">
- <p>some test some test</p>
- <p>some test some test</p>
- </div>
- </div>