select first element from current one with certain class

select first element from current one with certain class

Hi, I'm new to jquery and I have a problem.

here's the one of example codes from documentation:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <script src="http://code.jquery.com/jquery-1.5.js"></script>
  5. </head>
  6. <body>
  7.   <p>Hello</p>
  8.   <p class="selected">Hello Again</p>
  9.   <div><span>And Again</span></div>
  10. <script>$("p").next(".selected").css("background", "yellow");</script>

  11. </body>
  12. </html>
Now, this works fine so far, but let's say I insert <b>Hello to you too!</b> between Hello paragraph and paragraph with class "selected":
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <script src="http://code.jquery.com/jquery-1.5.js"></script>
  5. </head>
  6. <body>
  7.   <p>Hello</p>
  8.   <b>Hello to you too!</b>
  9.   <p class="selected">Hello Again</p>
  10.   <div><span>And Again</span></div>
  11. <script>$("p").next(".selected").css("background", "yellow");</script>

  12. </body>
  13. </html>
As soon as I add this line of code, .selected is not hi-lighted any more. Now, I understand that this is the way this function is supposed to work (guess so, otherwise it's a bug), but I couldn't find any function in documentation that would go from my current element to the first element that has that class applied and skip everything in between.

Thanks in advance.