How do I use jQuery .each() on a string
// For Exmaple
var mystring = '<div> bleh content </div> <div> bleh content </div>';
$('div', mystring).each(function(e) {
alert('do something');
});
// the above code isnt launching an alert for each div in the string? Im not sure why?
The way you’re doing it, you’re searching for
divelements inside the elements passed in. Basically equivalent of doing a.find().What you want is
filter()which will filter over the top level elements in the collection you passed.Test it here: http://jsfiddle.net/u5uDg/
If you wanted to use your approach, you would need to give the
divelements a parent on which jQuery can perform the find.http://jsfiddle.net/u5uDg/1/
As requested in your comment, you can delay execution of the code in the
.each()usingsetTimeout()and arriving at the duration of each by multiplying the current iteration number by the number of milliseconds you want to delay.http://jsfiddle.net/u5uDg/6/