How can I write this JavaScript iteration function in jQuery? -
i have js function returns list of ".youtube" elements, loops through selector, , calls function. javascript function working beautifully, want translate jquery. here's js function, works correctly:
var videos = document.queryselectorall(".youtube"); (var = 0; < videos.length; i++) { getvideos(videos[i]); } now here's attempt @ translating jquery:
$(".youtube").each(function(i, youtube){ getvideos(youtube[i]); }); this doesn't work. here's jsfiddle: http://jsfiddle.net/k3y3yvxq/2/
no need index in jquery each(), youtube refers element
the .each() method designed make dom looping constructs concise , less error-prone. when called iterates on dom elements part of jquery object. each time callback runs, passed current loop iteration, beginning 0. more importantly, callback fired in context of current dom element, keyword refers element. ( taken https://api.jquery.com/each/ )
$(".youtube").each(function(i, youtube){ getvideos(youtube); // or getvideos(this); });
Comments
Post a Comment