![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
PLEASE HELP! |
|
I would like to include a UL as a menu, styled by an included CSS Style Sheet. The problem I am having is how do I dynamically set the "active" page class using JavaScript to change each time I click on a new list item? I have found a bunch of examples but none that really accomplish what I need. As I choose from the list, I want the current page selected, evm.htm for example, to have the following automatically populate using JS so that the included menu HTML would look like this after tab 2 is selected: |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
if(window = document){ |
|
this.className=this.className=='active'?'active':' '; this.className+=this.className?' '+'active':'active'; |
#5
| |||
| |||
|
|
Response to MLD <mldboston (AT) gmail (DOT) com>: if(window = document){ Assignment, not comparison. this.className=this.className=='active'?'active':' '; this.className+=this.className?' '+'active':'active'; Find out how to use "this" properly. You are not referencing your LI's in this loop at all. |
#6
| |||
| |||
|
|
Response to "-Lost" <maventheextrawo... (AT) techie (DOT) com>: Response to MLD <mldbos... (AT) gmail (DOT) com>: if(window = document){ Assignment, not comparison. this.className=this.className=='active'?'active':' '; this.className+=this.className?' '+'active':'active'; Find out how to use "this" properly. You are not referencing your LI's in this loop at all. Oh, and for the record... I did not mean to imply that, that was all there was wrong with your code. -- -Lost Remove the extra words to reply by e-mail. Don't e-mail me. I am kidding. No I am not. |
#7
| |||
| |||
|
|
You are not adding any value or ANSWERING my questions or showing me HOW to do what I need to do... All you are doing is pointing out what I am doing wrong. thanks anyway. |
#8
| |||
| |||
|
|
MLD said the following on 1/2/2008 12:36 PM: You are not adding any value or ANSWERING my questions or showing me HOW to do what I need to do... All you are doing is pointing out what I am doing wrong. thanks anyway. Umm, go slap yourself and wake up. This is Usenyt, you ask a question, it gets discussed. If you get an answer, great. If you don't, then you don't. This is not your personal help desk. TGFY. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ -http://jibbering.com/faq/index.html Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/ |
#9
| |||
| |||
|
#10
| ||||||||||||
| ||||||||||||
|
|
MLD said: [snip] [Prepended line numbers] 1 function getCurrent() 2 { 3 * if(!document.getElementById || !document.createTextNode){return;} 4 * var n=document.getElementById('menu'); 5 * if(!n){return;} 6 * var lis=n.getElementsByTagName('li'); 7 * for (var i=0;i<lis.length;i++) 8 * { 9 10 * * *if(window = document){ 11 * * * * * * *this.className=this.className=='active'?'active': ''; 12 * * * * * * * * * * * * * * *this.className+=this.className?' '+'active':'active'; 13 * * *} 14 *} 15 } If I could get a little more clarification on exactly what it is you're trying to do in the evaluations in the for loop, I'd be happy to give you a hand. What is it you are trying to do with the two ternary operators in the middle there? |
|
Here are some things I did notice: globally: What is going to happen if the menu element is not found because it hasn't loaded into the DOM quite yet? Different browsers use onload differently, so it's a distinct possibility that the element would be there, just not yet, and then you'd skip over your whole function. |
|
The variable names are a tad on the obscure side. A little more descriptive naming will serve you well when you need to maintain this code in a month or six. "menu" instead of "n" "menuItems" instead of "lis" IMHO, of course. line 3: You don't call document.createTextNode in the function, so feature detection for it seems extraneous to your purpose. I think feature |
|
testing for document.getElementById is enough. And if I recall, there is * a better way than that, but I cannot recall. |
|
line 5: In checking for an element, I find that this serves me better: if (typeof n == "object") { * *// element is there and grabbed} else { * *// no element (or not element _yet_) |
|
} |
|
line 10: The single "=" in this line assigns instead of compares. In either case, |
|
this isn't going to help a bunch in checking to see if your elements are available. Check for the elements specifically, or for their next sibling to make sure they exist. |
|
line 11, 12: I think you're trying to say "if this li has no class, name it active, and if it has a class, append ' active' to it. If that is indeed the case, then don't overcomplicate your life with the ternaries when you're just starting out. A simple if would suffice: if (lis[i].className.length) { * *lis[i].className += ' active';} else { * *lis[i].className = 'active'; } |
|
I'm curious to know if this is a first attempt, and if so, you've come |
|
to the right place. I understand a lot of the things people say here can be upsetting at times, but Randy had it right when he told you this is |
|
Ok, enough pontification from me. Hope this helps. |
![]() |
| Thread Tools | |
| Display Modes | |
| |