Updated Jan 16th 2013: Added vanilla looping using forEach.
Listed below are a few nice vanilla js features you might not have used before.
Manipulating element classes
1 2 3 4 5 6 7
Query using css selectors
1 2 3 4 5 6
When you’re really attached to your jQuery / Prototype dollar shorthand, it’s easy to recreate it yourself using a simple function.
1 2 3 4
Loop over arrays using forEach
However this only works for arrays. DOM query functions like
querySelectorAll usually return a node list. These lists act like arrays but really aren’t. And most importantly they don’t have the function
forEach. To work around this and use
forEach on everything that acts like a collection use the
call function passing in the array and callback as arguments.
1 2 3