JavaScript DHTML/jQuery/prev
Find the very previous sibling of each paragraph that has a class "selected".
<source lang="html4strict">
<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>
Hello</div>
Hello Again
And Again
</body>
</html>
</source>
Get previous one
<source lang="html4strict">
<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>
asdf
asdf
</body>
</html>
</source>
Get previous tag
<source lang="html4strict">
<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>
asdf
asdf
asdf
asdf
asdf
asdf
asdf
asdf
<button>Go to Prev</button> </body>
</html>
</source>