Saturday, December 15, 2018

Use .each in jQuery without chaining it onto something.

Beyond this you can do stuff like this:

$.each(things, function (index, value) {
   alert("position " + index + " has: " + value);
});

 
 

Note that the first variable coming back from the each is an index and the second is the actual item in the collection. This messed with my mind some. Here is an example of the chaining:

$(".stuff").find("button").each(function (outerIndex, outerValue) {
   $(outerValue).children().each(function (innerIndex, innerValue) {
      if ($(innerValue).html() === "Bulk Paste") {
         $(outerValue).attr('style', "display: none;");
      }
   });
});

 
 

The parenthesis with leading dollar sign must wrap innerValue to do a .html() off of it. If you are struggling with getting jQuery methods to work, make sure you are not missing this piece.

No comments:

Post a Comment