Possible Duplicate:
jquery find closest previous sibling with class
The code given below is working perfectly
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div><span>Hello</span></div>
<p class="selected">Hello Again</p>
<p>And Again</p>
<script>$("p").prev(".selected").css("background", "yellow");</script>
</body>
</html>
But when i move .selected at the top(given below) then it is not working.Can anyone tell me how to make it to work for me and get the .selected element from .prev() method in the second case without manipulating html
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p class="selected">Hello Again</p>//Moved to top
<div><span>Hello</span></div>
<p>And Again</p>
<script>$("p").prev(".selected").css("background", "yellow");</script>
</body>
</html>