Despite its age, jQuery is still a popular library for manipulating the DOM.

In this article, we’ll look at some tips for working with jQuery.

Using jQuery .each() to Iterate Backwards

To call each to iterate through an array backward, we can use the reverse method to reverse the items.

For instance, we can write:

$($("li").get().reverse()).each(function() { 
  /* ... */ 
});

We get all the li elements, convert the Node list to an array with get .

Then we call reverse to reverse the items.

Now we can call each to iterate through each item in reverse.

We can also create our own reverse plugin to reverse the items.

For example, we can write:

$.fn.reverse = [].reverse;
$("li").reverse().each(function (i) {
    $(this).text(`item-${i}`);
});

We created the reverse jQuery plugin with by getting the reverse method of the array and returning it.

Then we can call reverse with our jQuery object.

Then that returns an array and we can call each on it.

#software-development #javascript #technology #web-development #programming

jQuery Tips — Slim Package, each and reverse, Copy to Clipboard
1.45 GEEK