Reputation: 151
I need help with following menu.
How would I get a particular submenu to stay open when a submenu item is clicked and another page is loaded.
Upvotes: 1
Views: 2435
Reputation: 2810
Does this work? You should be able to place it just above the hitMe function.
var url = document.location.toString();
var url_array = url.split("/");
// get last item in array
var last = url_array[url_array.length-1];
$('a').each( function() {
// if it matches
if ($(this).attr('href').indexOf(last) != -1) {
$(this).parent().parent().show();
$(this).css( { 'background': '#FFF4D2', 'color': '#333' });
}
});
This should take the value of the link's path and open/style the list.
EDIT:
If you go to http://jsbin.com/urupo3/5/edit and click "Preview," you can see that the code does appear to work as the link <a href="edit">
is highlighted and expanded.
Upvotes: -1
Reputation: 20210
If you reload the entire page, you have to submit some information to the new page. To achieve this, you have to modify the links of your sub menu items, or set a cookie. On page load you have to parse these information (url or cookie), and apply this to your menu. You could also use some server side code to have a pre-rendered HTML where your sub menu is already open.
In this case jsFiddle is not a good helper to show you how it could work.
EDIT:
You could do the following (untested):
Give your top menu <div>
a unique ID, i.e.
<div class="menutop" id="posts">
Add a hash to each URL of a sub item i.e.
<li><a href="/Post/New#posts">Add New</a></li>
Try this hash to open the menu
$(document.location.hash).click();
Upvotes: 5
Reputation: 2853
The code you provided works for me.
I forked your code and replaced the "Users \ Add new" option with "Users \ Google":
<li><a href="http://www.google.com/" target="_blank">Google</a></li>
http://jsfiddle.net/marcosfromero/UKvva/
EDIT
After reading another answer I figured out that you want to keep the submenu open after leaving your page. In that case, my answer doesn't help.
Upvotes: -1