Chain simple functions together - Help

Discussion in 'jQuery' started by Asmodean, Jul 8, 2012.

  1. #1
    I need to do the following:
    For all <p> elements that contain X in class name
    Find first <div> element after him
    Find first <a> element
    Set onclick function for that <a> element to toggle(<div>) element found above


    So in the following HTML code

    <p class='foo_1'>
    <a> bar </a> // a1
    </p>
    <div></div> // div1

    <p class='foo_2'>
    <a> bar </a> // a2
    </p>
    <div></div> // div2

    When i click on a1 i want function toggle to be called for div1, and when i click on a2 i want function toggle to be called for div2.

    I hope I phrased my question good enough so everyone can understand it. Any help would be greatly appreciated. Thanks :)
     
    Asmodean, Jul 8, 2012 IP
  2. stefek99

    stefek99 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $("p").hasClass("X").find("div").get(0).find("a").get(0)
    Code (markup):
    get(0) gets the first element...
     
    stefek99, Jul 25, 2012 IP
  3. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    This should do it:
    
    $('p[class*=X] a').click(function(){
       $(this).parent().nextAll('div:first').toggle();
    });
    
    Code (markup):
     
    Soulstone, Aug 14, 2012 IP