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:
- <!DOCTYPE html>
- <html>
- <head>
- <script src="http://code.jquery.com/jquery-1.5.js"></script>
- </head>
- <body>
- <p>Hello</p>
- <p class="selected">Hello Again</p>
- <div><span>And Again</span></div>
- <script>$("p").next(".selected").css("background", "yellow");</script>
- </body>
- </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":
- <!DOCTYPE html>
- <html>
- <head>
- <script src="http://code.jquery.com/jquery-1.5.js"></script>
- </head>
- <body>
- <p>Hello</p>
- <b>Hello to you too!</b>
- <p class="selected">Hello Again</p>
- <div><span>And Again</span></div>
- <script>$("p").next(".selected").css("background", "yellow");</script>
- </body>
- </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.