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
$("p").hasClass("X").find("div").get(0).find("a").get(0) Code (markup): get(0) gets the first element...
This should do it: $('p[class*=X] a').click(function(){ $(this).parent().nextAll('div:first').toggle(); }); Code (markup):