Solved

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

Posted on 2012-03-28
9
658 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

776 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