JavaScript Tutorial/jQuery/prev
Find the very previous sibling of each paragraph that has a class "selected".
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function showParents() {
$("p").prev(".selected").css("background", "yellow");
});
</script>
<style>
.selected { color:blue; }
</style>
</head>
<body>
<body>
<span>Hello</span></div>
<p class="selected">Hello Again</p>
<p>And Again</p>
</body>
</html>
Get previous one
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").prev(".selected").css("background", "yellow");
});
</script>
</head>
<body>
<body>
<div class="selected">asdf</div>
<div>asdf</div>
</body>
</html>
Get previous tag
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var $curr = $("#start");
$curr.css("color", "#f99");
$("button").click(function () {
$curr = $curr.prev();
$("div").css("color", "");
$curr.css("color", "#f99");
});
});
</script>
</head>
<body>
<body>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div id="start">asdf</div>
<div>asdf</div>
<button>Go to Prev</button>
</body>
</html>