One of the compounding issues is that you have what looks like a tab in your menu, but isn't. Here's the revised code:
$(document).ready(function() {
var acc1activetab=$('#acc1 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length;
var acc2activetab=$('#acc2 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length;
if (acc1activetab==1) acc1activetab=-1; //If second (inactive) tab, none are active
if (acc1activetab>1) acc1activetab-=1; // If more than second (inactive) tab, subtract a tab number
$("#acc2").accordion({
alwaysOpen: false,
autoheight: false,
header: 'a.acc2',
clearStyle: true,
active: acc2activetab
});
$("#acc1").accordion({
alwaysOpen: false,
autoheight: false,
header: 'a.acc1',
clearStyle: true,
active: acc1activetab
});
});
function GetCurrentPageName() {
//return 'Link1'; //hard coded for testing
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
var index = sPage.indexOf('.');
sPage = sPage.substring(0, index);
return sPage.toLowerCase();
}
$(document).ready(function() {
// var acc1activetab=$('#acc1 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length;
// var acc2activetab=$('#acc2 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length;
// if (acc1activetab==1) acc1activetab=-1; //If second (inactive) tab, none are active
// if (acc1activetab>1) acc1activetab-=1; // If more than second (inactive) tab, subtract a tab number
$("#acc2").accordion({
alwaysOpen: false,
autoheight: false,
header: 'a.acc2',
clearStyle: true,
active: '#acc2 > li:has(a[href*="'+GetCurrentPageName()+'."]) > a'
});
$("#acc1").accordion({
alwaysOpen: false,
autoheight: false,
header: 'a.acc1',
clearStyle: true,
active: '#acc1 > li:has(a[href*="'+GetCurrentPageName()+'."]) > a'
});
});
function GetCurrentPageName() {
//return 'Link7'; //hard coded for testing
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
var index = sPage.indexOf('.');
sPage = sPage.substring(0, index);
return sPage.toLowerCase();
}
Motley
Star
13789 Points
2449 Posts
MVP
Re: select current page in jquery accordion menu in master page
Apr 24, 2012 07:33 PM|LINK
One of the compounding issues is that you have what looks like a tab in your menu, but isn't. Here's the revised code:
$(document).ready(function() { var acc1activetab=$('#acc1 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length; var acc2activetab=$('#acc2 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length; if (acc1activetab==1) acc1activetab=-1; //If second (inactive) tab, none are active if (acc1activetab>1) acc1activetab-=1; // If more than second (inactive) tab, subtract a tab number $("#acc2").accordion({ alwaysOpen: false, autoheight: false, header: 'a.acc2', clearStyle: true, active: acc2activetab }); $("#acc1").accordion({ alwaysOpen: false, autoheight: false, header: 'a.acc1', clearStyle: true, active: acc1activetab }); }); function GetCurrentPageName() { //return 'Link1'; //hard coded for testing var sPath = window.location.pathname; var sPage = sPath.substring(sPath.lastIndexOf('/') + 1); var index = sPage.indexOf('.'); sPage = sPage.substring(0, index); return sPage.toLowerCase(); }And a link to jsfiddle http://jsfiddle.net/zbRFt/
Better code:
$(document).ready(function() { // var acc1activetab=$('#acc1 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length; // var acc2activetab=$('#acc2 > li:has(a[href*="'+GetCurrentPageName()+'."])').prevAll().length; // if (acc1activetab==1) acc1activetab=-1; //If second (inactive) tab, none are active // if (acc1activetab>1) acc1activetab-=1; // If more than second (inactive) tab, subtract a tab number $("#acc2").accordion({ alwaysOpen: false, autoheight: false, header: 'a.acc2', clearStyle: true, active: '#acc2 > li:has(a[href*="'+GetCurrentPageName()+'."]) > a' }); $("#acc1").accordion({ alwaysOpen: false, autoheight: false, header: 'a.acc1', clearStyle: true, active: '#acc1 > li:has(a[href*="'+GetCurrentPageName()+'."]) > a' }); }); function GetCurrentPageName() { //return 'Link7'; //hard coded for testing var sPath = window.location.pathname; var sPage = sPath.substring(sPath.lastIndexOf('/') + 1); var index = sPage.indexOf('.'); sPage = sPage.substring(0, index); return sPage.toLowerCase(); }And better jsfiddle: http://jsfiddle.net/zbRFt/1/