Jquery tabs with cookie support restore wrong tab position after page refresh.

Posted by zenonych on Stack Overflow See other posts from Stack Overflow or by zenonych
Published on 2010-02-24T10:49:01Z Indexed on 2010/04/06 6:43 UTC
Read the original article Hit count: 193

Filed under:

Hello, all. I have tricky problem which I can't completely understand... It's jquery tabs with cookie support. I've following code:

$(document).ready(function() {
var $tabs = $("#tabs").tabs();

$tabs.tabs('select', $.cookie("tabNumber"));

$('#tabs ul li a').click(function() {
    $.cookie("tabNumber", $tabs.tabs('option', 'selected'));
});

$('#btnSelect').click(function() {
    //alert($.cookie("tabNumber"));
    //$tabs.tabs('select', 2);
    $tabs.tabs('select', $.cookie("tabNumber"));
});

});

So, I've 3 tabs (with positions 0,1,2) inside div named "tabs". When user selects one tab, then tab position stores in cookie. After that if user refresh page, active tab position must be restored. But each time I refresh page I get active tab in previous position (if I select 2nd tab, then after refresh I got active tab in position 1, etc.). I add some test in code (button btnSelect with onclick handler which duplicates load position functionality). So, if I uncomment and use

$tabs.tabs('select', 2);

Then after I click btnSelect I've got right position. Ok, that's right. Then I comment that line and uncomment next one:

alert($.cookie("tabNumber"));

So, I select tab, click button, get dialog message "2", and after that tab in position 1 became active. Why?? In both cases I call 'select' method with parameter 2...

I know I can use aliases for tabs, but I want to understate why my code doesn't work properly.

© Stack Overflow or respective owner

Related posts about jQuery