Get each loop index : each « jQuery « JavaScript Tutorial






<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function () {
              $("div").each(function (index, domEle) {
                if ($(this).is("#stop")) {
                  $("span").text("#" + index);
                  return false;
                }
              });
            });
        });
    </script>
  </head>
  <body>
    <body>
        <button>Click</button> 
          <span></span>
          <div>asdf</div>
          <div>asdf</div>
          <div>asdf</div>
          <div>asdf</div>
          <div id="stop">Stop here</div>
          <div>asdf</div>
          <div>asdf</div>
          <div>asdf</div>

    </body>
</html>








30.32.each
30.32.1.Loop through each tag
30.32.2.each function callback
30.32.3.Each span tag
30.32.4.For each array element
30.32.5.For each function with index and value
30.32.6.Use Each Function To loop through Selected Item
30.32.7.Each function and array
30.32.8.Each function and map array
30.32.9.Use 'return' to break out of each() loops early.
30.32.10.Get each loop index
30.32.11.Use foreach to create li
30.32.12.Append value with each function
30.32.13.Return true as continue statement
30.32.14.Use each function to add class
30.32.15.Hard code value into each function