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
Solved

JQuery tabs - how do go to the selected tab after reloading a page

Posted on 2012-03-28
9
659 Views
Last Modified: 2012-03-31
I use the following script, but not working. I always go back to the first tab after reloading a page. For an example, If I select tab2, after reloading a page, i like to go back to selected tab2.
I looked at the JQuery menu, but i do not understand. Please advise and correct my mistake


$(function(){
      $('#tabs').tabs();
      var $tabs = $('#example').tabs();
      var selected = $tabs.tabs('option', 'selected');
      $tabs.tabs('select', selected);
});
</script>
0
Comment
Question by:unistudent
  • 4
  • 3
9 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 150 total points
ID: 37776138
see this for your reference:-
http://jqueryui.com/demos/tabs/
0
 

Author Comment

by:unistudent
ID: 37776160
I did that's why I added the following two lines


                        // Tabs
                      $('#tabs').tabs();
                      var $tabs = $('#tabs').tabs();
                      var selected = $tabs.tabs('option', 'selected');
                      alert("selected[" + selected + "]");
                       $tabs.tabs('select', selected);

after a page is reloaded, the selected var is set back to 0 "The first tab"
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37776194
$('#example').tabs({
    load: function(event, ui) {
        $(ui.panel).delegate('a', 'click', function(event) {
            $(ui.panel).load(this.href);
            event.preventDefault();
        });
    }
});
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 7

Accepted Solution

by:
designatedinitializer earned 350 total points
ID: 37776197
You'll have to store that info in a cookie and retrieve it on page load.
You can do this inside the 'select' event handler:
$.cookie("myselectedtab", $tabs.tabs('option', 'selected'));

Open in new window


and this, inside a $(document).ready:
$tabs.tabs('select',$.cookie("myselectedtab"));

Open in new window

0
 

Author Comment

by:unistudent
ID: 37780169
Hi designatedinitializer

would you give me an example of select event handler? Thanks
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 150 total points
ID: 37780315
did you try the script given by me... the key is to set preventDefault=false....
0
 

Author Comment

by:unistudent
ID: 37780388
I did try, but it still go back to the first tab after reloading the page. Sorry, it may not be a good question but this is my first time to use JQuery.

Below is my script and HTML.

<script type="text/javascript">
$(function(){

 $('#tabs').tabs();

 $('#tabs').tabs({
   load: function(event, ui) {
         $(ui.panel).delegate('a', 'click', function(event) {
            $(ui.panel).load(this.href);
            event.preventDefault();
         });
   }
  });

});
</script>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HTML codes

<div id="tabs">
  <ul>
     <li><a href="#tabs-1"">Sign Up</a></li>
     <li><a href="#tabs-2">Member Logon</a></li>
     <li><a href="#tabs-3">Forgot Password</a></li>
  </ul>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Would you please advise how to resolve this issue as I am very new in JQuery?
0
 

Author Comment

by:unistudent
ID: 37780462
I also try the following, but it still does not work.'


<script type="text/javascript">
$(function(){
      $(".selector").tabs({cookie: { expires:30 }});
      
      // Tabs
      $('#tabs').tabs();
      //hover states on the static widgets
      
      $(document).ready(function() {
         $("tabs").tabs('select', $.cookie("tabNumber"));
         $("tabs").click(function() {
           $.cookie("tabNumber", $("tabs").tabs('option', 'selected'));
         });
      });
      
      $('#dialog_link, ul#icons li').hover(
            function() { $(this).addClass('ui-state-hover'); },
            function() { $(this).removeClass('ui-state-hover'); }
      );
      
      $('#tabs').tabs({
          load: function(event, ui) {
              $(ui.panel).delegate('a', 'click', function(event) {
                  $(ui.panel).load(this.href);
                  event.preventDefault();
              });
          },
      });

});
</script>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery-Javascript setAttribute function help 7 18
Google Analytics setup 5 50
ASP.NET Content Page 3 29
Html Newline 7 21
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 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