Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <body>
    <p class="demo">1</p>
    <p class="demo">2</p>
    <p class="demo">3</p>
    <script>
      var cars = ["Saab", "Volvo", "BMW"];
      var para = document.getElementsByClassName("demo");
      for (var k = 0; k < cars.length; k++) {
        (function (k) {
          para[k].onclick = function () {
            this.innerHTML = cars[k];
          }
        }(k));
      }
    </script>
  </body>
</html>
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers