Set style to previous element in click event

Description

The following code shows how to set the previous element in click event handler.

Example


<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {<!--  w  w w .j  a  v  a 2  s  .  c  o m-->
    var $curr = $("#start");
    $curr.css("color", "#f99");
    $("button").click(function() {
      $curr = $curr.prev();
      $("div").css("color", "");
      $curr.css("color", "#f99");
    });
  });
</script>
</head>
<body>
  <div>java2s .com</div>
  <div>java2 s.com</div>
  <div>java 2s.com</div>
  <div>jav a2s.com</div>
  <div>ja va2s.com</div>
  <div>j ava2s.com</div>
  <div id="start">java2s.com</div>
  <div>java2s.com</div>
  <button>Go to Prev</button>
</body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities