views:

145

answers:

1

Hello, again.

I have a few list elements, like this:

<li class="item">
    <a class="toggle"><h4>ämne<small>2010-04-17 kl 12:54 by <u>nike1</u></small></h4></a>
    <div class="meddel">
        <span>
            <img style="max-width: 70%; min-height: 70%;" src="profile-images/nike1.jpg" alt="" />
            <a href="account.php?usr=47">nike1</a>
        </span>

        <p>text</p>
        <span style="clear: both; display: block;"></span>
    </div>
</li>

<li class="item odd">
    <a class="toggle"><h4>test<small>2010-04-17 kl 15:01 by <u>nike1</u></small></h4></a>
    <div class="meddel">
        <span>
            <img style="max-width: 70%; min-height: 70%;" src="profile-images/nike1.jpg" alt="" />
            <a href="account.php?usr=47">nike1</a>
        </span>

        <p>test meddelande :) [a]http://youtube.com[/a]&lt;/p&gt;
        <span style="clear: both; display: block;"></span>
    </div>
</li>

And i'm trying to figure out how to make it so that when you click a link with the class .toggle, the parent element (the li element) will toggle the class .current.

I'm not sure if the following code is correct or not, but it's not working.

$(this).parent('.item').toggleClass('.current', addOrRemove);

Even if i remove "('.item')", it doesn't seem to make any difference.

So, any ideas?

Thanks in advance, -Nike

+2  A: 

toggleClass takes a class name, not a selector. Therefore, you shouldn't include a ..
Change it to toggleClass('current') (without a .).

Also, you can make your code more robust by calling closest instead of parent. The closest will search for the innermost parent element that matches the selector, so that it will keep working if the link is nested inside the .item.

SLaks
Thank you, still doesn't seem to be working though :/ $('.pmlist ul li .toggle').click(function() { $(this).next("div").toggle(250); $(this).closest('item').toggleClass('current'); });The toggle function before triggers, it's just the toggleClass function that for some reason isn't working.
Nike
Kind of crappy that you can't post code blocks as comments, or even add new lines :S Hope you can read it anyways :P
Nike
Can you see the class in Firebug?
SLaks
Nope, nothing new appears.
Nike
Try `console.log($(this).parent('.item'))` to check whether the selector matched.
SLaks
@Nike - `.closest()` uses a selector, so it *does* need the `.` in `.item`, so your code should be: `$('.pmlist ul li .toggle').click(function() { $(this).next("div").toggle(250); $(this).closest('.item').toggleClass('current'); });`
Nick Craver
Thank's Nick, that solved it! :)
Nike
Done. . . . . . . . .
Nike