views:

129

answers:

1

Hello! I have a sidebar navigation that lists blog entry titles down the side of the page. I am trying to write some jquery that will check the text in the title of the current full blog entry page and match it with the corresponding title in the sidebar navigation so that I can apply a class to style for an active state link ... but I'm not quite getting it! Here is the link to an example page: http://ncw-commercial.com/property-listings/eastpoint-plaza-lot.html, and below is my current code. I have also tried using :contains but could not figure out how to get that to work with a variable rather than direct text.

$('.single-journal-entry-wrapper .journal-entry .title').each(function(){ 
      var activeTitle = $(this).text();
      $(".feedburnerFeedBlock .headline a").filter(function(index) {
         return $(this).text() == "activeTitle";
      }).parent().addClass("activeTitle");
}); 
+1  A: 

I think you are very very close (assuming the selectors are correct). Just remove the quotes around activeTitle (otherwise you are comparing against the string "activeTitle"):

$('.single-journal-entry-wrapper .journal-entry .title a').each(function(){ 
      var activeTitle = $(this).text();
      $("#sidebar1 .section .headline a").filter(function(index) {
         return $(this).text() == activeTitle;
      }).parent().addClass("activeTitle");
});

You can also use the :contains() selector:

$('.single-journal-entry-wrapper .journal-entry .title a').each(function(){ 
      var activeTitle = $(this).text();
      $("#sidebar1 .section .headline a:contains('" + activeTitle + "')")
      .parent().addClass("activeTitle");
});

The link to the page is not helping much. Somehow I don't see which title you want to match with which link. Sorry.

Update:

Ok I figured it out. The problem was, that both strings (the title and the one in the link didn't match because the title had a   in it (Eastpoint Plaza Lot) where the link in the sidebar had not. And although this is converted to a blank if you call .text(), it somehow does not match.

Now this could be solved by get the title via .html() and replace the   with a space:

$('.single-journal-entry-wrapper .journal-entry .title a').each(function(){ 
      var activeTitle = $(this).html().replace(' ', ' ');
      $("#sidebar1 .section .headline a:contains('" + activeTitle + "')")
      .parent().addClass("activeTitle");    
});

For testing I copied all your HTML code and it works for me now. Give it a try. I think the biggest Problem is, that titles may have HTML encoded characters in it, so find a library that convert them if you run into more problems.

Felix Kling
in the right sidebar the entry titles for the section are listed (right now there are only two), so in this example I need the link in the sidebar that matches the entry title ("Eastpoint Plaza Lot").It's weird because it really seems like I have it, tried both of the suggestions above but still not getting the class applied to the sidebar link.
VUELA
@VUELA: I updated my answer. I couldn't find an element with class `feedburnerFeedBlock` but maybe I just didn't see it. Anyway, I changed the selectors, give it a try.
Felix Kling
I appreciate you taking a look at this! I realized a little while ago that I need to use .single-journal-entry-wrapper .journal-entry .title a -- so that has been updated, but changing the selector for the sidebar didnt help.
VUELA
@VUELA: Does it actually run? What if you put `alert($(this).text())` into the first line in the function? (just to make sure, that the title is selected correctly)
Felix Kling
yes, this and some other tests i've tried all run fine. could the problem be that the sidebar links list is actually added to the page via feedburner script to insert the rss feed as html? maybe the links arent there when the jquery runs?
VUELA
@VUELA: This is a very good point. That is probably the reason why I didn't see them at the beginning (I have most JS disabled by default). Somehow you have to put this code after the feedburner stuff.
Felix Kling
I have the feedburner script embedded in the html for the sidebar. And I currently have the jquery in the <head>. I tried moving it into the sidebar html underneath the feedburner script but that didnt work either. hmmmmm.
VUELA
maybe i can somehow use getScript();
VUELA
What does `$("#sidebar1 .section .headline a").each(function(){alert($(this).text())});` give you? To make sure that the sidebar links are selected correctly.
Felix Kling
it pretty much gives me the correct values (it also adds in the sidebar links for hidden navigation for other sections which still works anyway). and if i revise it to $("#sidebar1 #sectionContent2830995 .headline a").each(function(){alert($(this).text())}); then it gives exact results for the visible sidebar section.
VUELA
so I guess that means that the content is accessible even though it's loaded from the feedburner script .... so i'm at a loss for why it doesnt work!
VUELA
@VUELA: See my updated answer I think I figured it out :)
Felix Kling
you rock. thanks so much!!!
VUELA
what's killing me though is that for some reason the editor in the cms i am using automatically changes the   to a space. so everytime i go back into the editor i have to put   back in. weird. do you think there is a fix for this?
VUELA
@VUELA: Do you mean you have to put it in because you want it to be there? You know, for the `replace` method it does not *have* to be there. But no, every editor is somewhat different so I cannot know.
Felix Kling
no i mean in the jquery script. when i use the revised script your provide above my cms editor converts the   into a space. so the first time i do it works great, but the next time i open the editor it is a blank space and not the characters for the space so i have to paste   into the script everytime i edit anything in my <head> area. really dumb that it's setup that way.
VUELA
i ran into this proble before with this cms editor ... just trying to remember what the solution was ... im thinking it was something to with using <![CDATA[ ... ]]> so im tinkering with that idea.
VUELA
@VUELA: Ah ok, I didn't know that you edit this code from CMS. Maybe wrapping it in `<!-- //-->` helps.
Felix Kling
the whole script was originally wrapped in <!-- //--> so that wasnt helping. i have now also tried all the cdata options listed here: http://javascript.about.com/library/blxhtml.htm
VUELA
maybe ill just put it all in an external file.
VUELA
@VUELA: I think in this case, this is the best idea.
Felix Kling
not working so far ... but im sure that is the solution to use an external file or somehow make sure that space character does not keep getting converted back! anyway ... thanks so much for the help!! ... ill post back when i have it working!!
VUELA
@VUELA: Do that :) Good luck!
Felix Kling
cool. all good now. as soon as i moved it (correctly) into an external file the cms editor was no longer able to get in my way!! thanks again!
VUELA
@VUELA: Yeah I checked your site, looks good :) Btw I noticed that always the last space in the title is a ` `. I guess this is also something the CMS is doing (to avoid having a single word in the next line maybe). Anyway, you're welcome :)
Felix Kling
yep, that's exactly it.
VUELA
thanks, not my site though -- just helping out a client with one of their sites. my work can be seen in my portfolio at http://www.vueladesign.com -- i pretty much always build using this cms (squarespace.com), since im not a developer really.
VUELA