You could have an issue with the ondomready code executing before you're included file builds the navigation, depending upon how you're including it. I'd suggest using Firebug for Firefox instead of the alert() and adding console.log() in your code to make sure you've actually got the DOM you think you should when the code is executing.
On Mar 15, 3:54 pm, vintagetwitch <ksandn...@gmail.com> wrote: > Hmmm... > > Still no luck. I suspect this has more to do with me. Also to give a > little background, I have 40 html pages that I'm using a jquery > styleswitcher script on to be able to switch between 4 different font > treatments. I am generated the nav html with Javascript like this if > that makes any difference so the navigation could be like an include > file, and I would only need to make edits in one place. I'm just > hoping to add a selected class to give some kind of visual cue as to > the current page being viewed. > > $('body').append('<ul class="design_html_nav"><li><a href="#" > rel="screen_verdana" class="styleswitch">Verdana</a></li><li><a > href="#" rel="screen_lucida" class="styleswitch">Lucida Unicode</ > a> ... .... > > And again, this is what I'm using to add the 'selected class. > > $(document).ready(function(){ > var file = jQuery.url.attr("file"); > alert(file); > $('.design_html_nav li').each(function(){ > var a = $(this).find('a'); //<--------get the anchor under > the current LI > alert(a[0]); > if(a[0].href===file){ //<-------------since there's only one > anchor, we can access the element in the jQuery object by [n] notation > and get its href attribute* > a.addClass('selected'); //<---------------------call > the addClass() method on the jQuery object containing the anchor > } > }); > > }); > > They are in separate files. > > Thanks so much, Kevin > > On Mar 12, 10:20 am, mkmanning <michaell...@gmail.com> wrote: > > > Last time :( > > You need the a variable as a jQuery object to call the addClass() > > method > > The a[0].href gets the href (duh), or you could use a.attr('href') > > > var file = jQuery.url.attr("file"); > > alert(file); > > $('.design_html_nav li').each(function(){ > > var a = $(this).find('a'); > > if(a[0].href===file){ > > a.addClass('selected'); > > } > > }); > > > On Mar 12, 10:00 am, mkmanning <michaell...@gmail.com> wrote: > > > > Sorry, it should just be if(a===file){.. > > > > the var 'a' is already the href. > > > > On Mar 12, 8:15 am, vintagetwitch <ksandn...@gmail.com> wrote: > > > > > Thanks for your response, and my apologies for the double post. > > > > So now, this is what I have, and for some reason, now I'm not able to > > > > extract any values from the array of <a> tags, even with an alert. > > > > Don't I need to loop through them to see if 'a === file'. > > > > > var file = jQuery.url.attr("file"); > > > > alert(file); > > > > $('.design_html_nav li').each(function(){ > > > > var a = $(this).find('a').attr('href'); > > > > if(a.href===file){ > > > > a.addClass('selected'); > > > > } > > > > }); > > > > > cheers > > > > > On Mar 11, 6:14 pm, mkmanning <michaell...@gmail.com> wrote: > > > > > > Deja Vu :) > > > > > > Why use a separate array? > > > > > $('.design_html_nav li').each(function(){ > > > > > var a = $(this).find('a').attr('href'); > > > > > if(a[0].href===file){ > > > > > a.addClass('selected'); > > > > > } > > > > > > }); > > > > > > On Mar 11, 3:36 pm, ksandn...@gmail.com wrote: > > > > > > > Hi there, > > > > > > > What I'm trying to do as add a 'selected' class to the current page > > > > > > navigation link. I want to cycle through all the page links and if > > > > > > 'file' matches 'hrefs', add the class accordingly. Specifically, I'm > > > > > > having problems with the last part of code below ---if (hrefs[i] == > > > > > > file) {$(this).addClass('selected');}---. I can't seem to isolate > > > > > > the > > > > > > matching link. > > > > > > I no javascript pro, so I'm probably approaching this all wrong. > > > > > > > My code is below. The alerts are just there to make sure the > > > > > > variables > > > > > > are storing the values I want. > > > > > > > var file = jQuery.url.attr("file"); > > > > > > alert(file); > > > > > > > var hrefs = new Array(); > > > > > > $('.design_html_nav li').each(function(){ > > > > > > hrefs.push($(this).find('a').attr('href')); > > > > > > }); > > > > > > alert (hrefs[6]); > > > > > > > for(i=0;i<hrefs.length;i++) > > > > > > { > > > > > > if (hrefs[i] == file) {$('.design_html_nav > > > > > > li a').addClass > > > > > > ('selected');} > > > > > > } > > > > > > > Thanks, Kevin