/    Sign up×
Community /Pin to ProfileBookmark

Prototypying Multi-tiered Navigation Function

Hi. I am trying to write a function to write a navigation as an [B]unordered list[/B], which I could then use css to display as tabs, or whatever. Remember this is for prototyping, not for a production site–and thus would like to use JS, not a server side language.

I would like a two tiered navigation, but not sure what data structure to use.

I got a one level navigation working by using an object as a sort of hash.

2 things I would love suggestions for:

1) The method nav.print() takes a parameter which when supplied adds a current class to the <a> tag. But can I make it so that if the name of that nav item changes in the items object, I won’t have to change the parameter name in the nav.print method call?

2) Can anyone suggest a data structure to support an unliminted number of sub nav items for each nav item?

[CODE] var nav = {
navContainer : $(‘nav’), //getElementById
items : {
“Home”: ‘index.php’,
“Resources”: ‘resources.php’
},
print: function(currName){
var frag = document.createDocumentFragment();
var ul = document.createElement(‘ul’);
for (var name in this.items){
var item = document.createElement(‘li’);
var link = document.createElement(‘a’);
var str = document.createTextNode(name);
link.setAttribute(‘href’, this.items[name]);
if (currName == name) link.setAttribute(‘class’,’current’);
link.appendChild(str);
item.appendChild(link);
ul.appendChild(item);
}
frag.appendChild(ul);
this.navContainer.appendChild(frag);
}
}
nav.print(‘Home’); //create the nav with ‘Home’ as the current item[/CODE]

[URL=”http://www.cohencentral.com/projects/js_nav/”]http://www.cohencentral.com/projects/js_nav/[/URL]
Any advice is appreciated.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @askohen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...