I thought that I had to push/collect all the anchor tags containing the specified condition (in this case all anchor tags that have an attribute of src that ends with '.pdf') in an empty array myself in order to work with them... but apparently not. Seems like the querySelectorAll() & for loop already take care of that if I'm understanding that correctly?

And the jQuery result will show two prepended PDF icons.

@Frank S,

Sure, please see attachment. Thanks for the effort!

sjondoe
—
2014-06-18T14:38:03Z —
#8

WolfShade said:

Somewhere in there, alert the length of anchor, just to make sure you're getting more than the last one.

...

^_^

I will get two results if I do a

console.log(anchor);

and that's correct, so how and why is it skipping to the last result?

WolfShade
—
2014-06-18T14:38:55Z —
#9

According to MDN, querySelectorAll() returns a list (not an array, as I mistakenly assumed) of all matching selectors. Maybe pushing the list into an array then looping the array?

HTH,

^_^

Pullo
—
2014-06-18T14:48:41Z —
#10

Hi,

Are you trying to write this function as an exercise?If not, you can use:

parent.insertBefore(el, parent.firstChild);

sjondoe
—
2014-06-18T14:54:42Z —
#11

Hi Pullo,

Yes it's an exercise. It's more important for me to know how and why something is done the way it's done in pure/modern JS.

WolfShade
—
2014-06-18T14:59:19Z —
#12

anchor.split(',') should turn the list into an array without looping. Then you can loop the array and insertBefore().

Thank you for helping me understand iteration in JS a little bit better and also thanks for the interesting resource.Also thanks for showing me a shorter way with forEach! Very interesting and much appreciated. Keeps me motivated to do more with JS.