Solved

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

Posted on 2012-03-28
9
655 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now