Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing location.hash with jquery ui tabs

I've been trying to find a way to change the window.location.hash to the currently selected tab in Jquery UI Tabs.

I've tried:

$("#tabs > ul").tabs(); $("#tabs > ul").bind("tabsshow", function(event, ui) {  window.location.hash = ui.tab; }) 

This results in changing the hash to #undefined when the tab is changed.

I've also tried:

$("#tabs > ul").tabs({  select: function(event, ui) {  window.location.hash = ui.tab } }); 

But this doesn't seem to be triggered at all.

Any help would be appreciated. Thanks.

Edit: It looks like part of my initial problem had something to do with js somewhere else interfering with this. Both the accepted answer and the other suggested answer slightly modified do work. Thanks all.

like image 404
Rob Avatar asked Feb 20 '09 16:02

Rob


2 Answers

In your event handler function ui.tab is an anchor element. You can retrieve its hash value like this:

$("#tabs > ul").tabs(); $("#tabs > ul").bind("tabsshow", function(event, ui) {      window.location.hash = ui.tab.hash; }) 

Does this work for you?

like image 107
Serxipc Avatar answered Oct 01 '22 18:10

Serxipc


$('#tabs').tabs({     select: function(event, ui) {         window.location.hash = ui.tab.hash;     } }); 
like image 23
Herman van der Meulen Avatar answered Oct 01 '22 18:10

Herman van der Meulen