... var txt = $(this).parents('div:first').find('p').text(); ... or ... var txt = $(this).parents('div:first').children('p').text(); ... or ... var txt = $(this).parents('span:first').siblings('p').text(); ... or ... var txt = $(this).parent().parent().siblings('p').text(); ... etc etc
Karl Swedberg-2 wrote: > > > > On Aug 25, 2007, at 1:10 AM, atomicnuke wrote: > >> >> <script type="text/javascript" src="jquery-1.1.4.js"></script> >> <script type="text/javascript"> >> >> $(document).ready(function(){ >> $("#comments span h5 a").click(function(){ >> var author = $(this).parent("span").next("p").text(); >> alert(author); >> }) >> }); >> </script> >> </head> >> >> <body> >> <div id="comments"> >> <div id="5"><h4>Minion</h4><h5>( # Reply )</h5></ >> span><br> >> <p>Back to normal status</p> >> </div> >> </div> >> >> I'm trying to figure out how to grab the text between the <p> tags >> when reply is clicked. I know I got the reply link targeted. I first >> just had an alert, but tried going further. At first I just had $ >> (this).next("p").text(); but thought maybe I needed to go to the >> parent, then move on, but it just shows a blank alert box. >> > > > You could try something like this: > $(document).ready(function() { > $('h5 > a').click(function() { > var txt = $(this).parents('div:first').next().text(); > alert(txt); > return false; > }); > }); > > This goes "up" the DOM to the first div that is an ancestor of the > clicked item, then goes to the next sibling and grabs its text. > > > --Karl > _________________ > Karl Swedberg > www.englishrules.com > www.learningjquery.com > > > > -- View this message in context: http://www.nabble.com/Help-Traversing-tf4332092s15494.html#a12340253 Sent from the JQuery mailing list archive at Nabble.com.