views:

54

answers:

4

I have a simple drop-down menu:

<ul id="nav" >
<li><a href='/'>Parent One</a>
<ul>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
</ul>
</li>
<li><a href='/'>Parent Two</a>
<ul>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
</ul>
</li>

I need to assign a class to the < li > tag of the parent that is equal to the name of the parent in the < a > tag.

<ul id="nav" >
<li class="ParentOne"><a href='/'>ParentOne</a>
<ul>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
</ul>
</li>
<li class="ParentTwo"><a href='/'>ParentTwo</a>
<ul>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
<li><a href='/'>child</a></li>
</ul>
</li>

I tried this bit of code:

    <script language="javascript" type="text/javascript">
 $('#navigation ul li a').each(function(index) {
 var link = $(this);
 $('li', link.parent()).addClass(link.text());
 });
 </script>

But that applies the class to the children's < li > tag and not the parent < li > - any help would be appreciated. Thanks!

+1  A: 

try this:

$(document).ready(function(){
    $('#nav>li>a').each(function(){
        $(this).parent().addClass($(this).text().replace(/\s/,''))
    });
});

:)

EDIT: Not thinking... omg... repaired :)

EDIT 2: Added white-space-trimming.

EDIT 3: Edited, added $document.ready, 100% working with white-space trimming ;)

Adam Kiss
close, but it returns class="" - so it's not grabbing the contents of the < a >
Melissa
where do you test? I just tested and returned what I wanted :) Sry, what YOU wanted :D
Adam Kiss
oops sorry, my mistake, copied wrong part of your quenstion - copied the wanted result :D
Adam Kiss
Melissa, maybe you forgot to wrap this javascript-code in `$(document).ready(function(){ ... });`
Harmen
@Harmen - my mistake - she has ul with id `nav`, so i should call `$('#nav li ul')`, but i called `$('#navigation ul li ul')` :D
Adam Kiss
thanks for all the tips! I actually have a #navigation div around the #nav ul - but it still only returns a class=""
Melissa
OH the new code you posted is working - sorry I didn't notice you updated it. first time here and the answer order was confusing me!
Melissa
Now click on the approval :) And I'm glad it worked. :)
Adam Kiss
+1  A: 

This should do it:

$(document).ready(function() {
    $('#nav > li > a').each(function(index) {
        $(this).parent().addClass($(this).text());
    });
});

That will also only act upon the top level li elements; in case you add classes to other deeper nested elements.

Mark B
Wow, you beat me in just 22 seconds... Except that your example doesn't remove the whitespaces
Harmen
Yeah, I was slightly confused, cause the second example given has no spaces in the link text...
Mark B
+3  A: 
$('#nav > li > a').each(function(){
    $(this).parent().addClass($(this).text().replace(/\s/, ''));
});
Harmen
A: 

This worked:

$("#nav li").each(function() { 
  $(this).addClass($(this).children("a").text());
});
Alex LE