Last Updated: September 09, 2019
·
1.648K
· jandudulski

Shorter $.each

In most cases when you are going to use $.each method you just want to call simple function on each element of the set.

In such cases instead of:

$('.nice-class').each(function(index, Element) {
  Element.doSmth();
});

You can just write:

$('.nice-class').each(doSmth);

Examples?

var hide_it = function() { $(this).hide(); };
$('[data-hidden]').each(hide_it);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside