Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Last Element ID</title>
  <script>
    function setUpParentDiv() {
      var div = $(document.body.lastChild).closest('div');
      switch (div.attr("id")) {
        case "div1":
          div.css("color", "blue");
          break;
        case "div2":
          div.css("color", "green");
          break;
      }
    }
  </script>
</head>
<body>
  <div id="div1">
    I'm div1
    <script>
      setUpParentDiv();
    </script>
  </div>
  <div id="div2">
    I'm div2
    <script>
      setUpParentDiv();
    </script>
  </div>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers