Love the fact that the for loops are now iterations in jQuery. The old way of doing loops like this:
var $elements = $(".myclass"); for (var x=0; x< $elements.length; x++){ $elements[x].toggleClass("shadow); }
Can now look like this in jQuery 3:
var $elements = $(".myclass"); for (var element of $elements){ element.toggleClass("shadow); }
This also works (since jQuery 1.0), right?
var $elements = $(“.myclass”);
$elements.each(function(){
$(this).toggleClass(“shadow);
});
LikeLiked by 1 person
I like that even better!
LikeLike
same as for each in java syntax.
LikeLike
Why not
$(“.myclass”).each(function() {
$(this).toggleClass(“shadow);
});
LikeLiked by 1 person
The new syntax avoid the function() part. Looks cool to me!
LikeLike
I’d prob stick with an Each loop as being less code. Wonder the rationale behind this syntax – first glance looks like a strange hybrid of For and Each loop – although don’t really see the value, unless planning to phase out Each!
LikeLike
It’s something similar to AngularJs foreach.
LikeLike
Why not do:
$(“.myclass”).toggleClass(“shadow);
(write less, do more)
LikeLike
Right but that is for a basic use case.
LikeLike