Development jQuery: simplify this?

Discussion in 'Tech Support' started by jezmck, 25 Jan 2009.

  1. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    Code:
    $(".menu a[@href$=cat/5]").parent().addClass("active").parents("ul").show().addClass("open");
    $(".menu a[@href$=cat/5]").parents("li").addClass("open");
    Can this be simplified to a single line? (or in anyway?)

    Relevant HTML:
    Code:
    <ul class="menu hier open">
     ...
     <li id="menu2" class="parent open">Peripherals
      <ul>
       <li id="menu3"><a href="/cat/3">Input</a></li>
       <li id="menu4" class="parent">Output
        <ul>
         <li id="menu5"><a href="/cat/5">CRT/LCD Monitor</a></li>
         <li id="menu6"><a href="/cat/6">VFD/LCD Panel</a></li>
         ...
        </ul>
       </li>
      </ul>
     </li>
     ...
    </ul>

    Any suggestions welcome :)

    see http://beta.scc.jezmckean.com/cat/5
     
  2. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    I assume you're generating the jQuery dynamically based off the current URL, right? Wouldn't it be easier to just add the classes dynamically to the list instead, or is that not a feasable option?

    You can try
    Code:
    $(".menu a[@href$=cat/5]").parent().addClass("active").parents("ul, li").show().addClass("open");
    but no guarantees. If that dual selector doesn't work, I don't think it's otherwise possible without needlessly complex DOM traversal.
     
  3. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    Yep, it should be but the function is recursive and complex enough already.
    Will give it a go.
    Okay, many thanks. :D
     

Share This Page