views:

69

answers:

2

I have a page of search results, each of which has an icon to add each result to a group (listed on the page in a hidden UL element: display:none;).

Right now it works... but when I click on the icon for one result listing... the UL appears under every single result.

I need it to only show up for the result listing that I am clicking the icon on... not all the listings...

Currently the onload code is this:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $("ul.quicklist_menu").css('display','block');  
        $(this).addClass("current");
    },
    function(){
        $("ul.quicklist_menu").css('display','none');
        $(this).removeClass("current");
});

I tried to change it to this:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).children("ul.quicklist_menu").css('display','block');   
        $(this).addClass("current");
    },
    function(){
        $(this).children("ul.quicklist_menu").css('display','none');    
        $(this).removeClass("current");
});

But, yeh.. the icon is highlighted as "current" or active, but the UL does not appear...

Any help would be greatly appreciated.

OOPs. Sorry! Here is the HTML:

<li class='vcard'>
    <a href='profile.php?id=1288'><img class='user-photo' src='profile_images/sq_avatar.png' alt='Paul Dietzel' /></a>
    <div class='user-info'>
        <a class='fn url name' href='profile.php?id=1288'>Paul Dietzel</a>
        <ul class='attributes'>
            <li class='adr'><span class='locality'>Los Angeles</span>, <abbr class='region' title='California'>CA</abbr></li>
        </ul>
    </div>
    <ul class='options'>
        <li><a class='view-profile' href='profile.php?id=1288'>View profile</a></li>
        <li><abbr title="Add/Remove Favorite"><button class="favorite ICON" id="1288">Remove Favorite</button></abbr></li>
        <li><a class='add-to-group' href='#newgrp'>Add to group</a></li>
        <ul class="quicklist_menu"><li><a href='#addgrp' id='1147' rel='1988'>Test 3</a></li>
            <li><a href='#addgrp' id='1147' rel='1989'>Test 4</a></li>
            <li><a href='#addgrp' id='1147' rel='1990'>Test 5</a></li>
            <li><a href='#addgrp' id='1147' rel='1991'>Test 7</a></li>
            <li><a href='#addgrp' id='1147' rel='1129'>Lou Herthum Acting Class</a></li>
            <li><a href='#addgrp' id='1147' rel='1967'>test group</a></li>
            <li class="mgrp"><a href="groups.php#newgrp">Manage Groups &raquo;</a></li>
        </ul>
    </ul>
</li>
+4  A: 

You need to correct your markup by moving that <ul> to be a sibling of the anchor, not outside the <li></li> but inside (<ul> can't be a child of a <ul>, it's invalid HTML). Like this:

<ul class='options'>
  <li><a class='view-profile' href='profile.php?id=1288'>View profile</a></li>
  <li><abbr title="Add/Remove Favorite"><button class="favorite ICON" id="1288">Remove Favorite</button></abbr></li>
  <li><a class='add-to-group' href='#newgrp'>Add to group</a>
    <ul class="quicklist_menu">
      <li><a href='#addgrp' id='1147' rel='1988'>Test 3</a></li>
      <li><a href='#addgrp' id='1147' rel='1989'>Test 4</a></li>
      <li><a href='#addgrp' id='1147' rel='1990'>Test 5</a></li>
      <li><a href='#addgrp' id='1147' rel='1991'>Test 7</a></li>
      <li><a href='#addgrp' id='1147' rel='1129'>Lou Herthum Acting Class</a></li>
      <li><a href='#addgrp' id='1147' rel='1967'>test group</a></li>
      <li class="mgrp"><a href="groups.php#newgrp">Manage Groups &raquo;</a></li>
    </ul>
  </li>
</ul>

Then you can use .next() like this:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).addClass("current").next("ul.quicklist_menu").show();  
    },
    function(){
        $(this).removeClass("current").next("ul.quicklist_menu").hide();
});

This uses the .show() and .hide() shortcuts to apply the display as well. Alternatively, you can use .click() to toggle using .toggleClass() and .toggle(), like this:

$("a.quicklist, a[href=#newgrp]").click(function() {
  $(this).toggleClass("current").next("ul.quicklist_menu").toggle();
});
Nick Craver
Thank you! Corrected the markup and used the first toggle and it works like a charm.
Paultwo
+1 Good catch on the proper `<ul>` nesting.
patrick dw
A: 

use .closest() and .next()

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).closest('li').next("ul.quicklist_menu").css('display','block');   
        $(this).addClass("current");
    },
    function(){
        $(this).closest('li').next("ul.quicklist_menu").css('display','none');    
        $(this).removeClass("current");
});

or you may also try like this,

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).addClass("current")
            .closest('ul.options')
            .children("ul.quicklist_menu")
            .css('display','block');
    },
    function(){
        $(this).removeClass("current")
            .closest('ul.options')
            .children("ul.quicklist_menu")
            .css('display','none');    
});
Reigel