Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .each() with string

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?

like image 518
kr1zmo Avatar asked Jun 30 '10 22:06

kr1zmo


People also ask

What is the use of jQuery each () function?

The $.each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

Can we use foreach in jQuery?

Learn how to loop through elements, arrays and objects with jQuery using the $. each() function, jQuery's foreach equivalent. jQuery's foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.

Is jQuery each asynchronous?

The jQuery. each method loops Synchronously, but you can't guarantee that it'll loop through the items in any specific order.

How do you iterate through an element in jQuery?

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0.


2 Answers

The way you're doing it, you're searching for div elements 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/

var mystring = '<div> bleh content </div> <div> bleh content </div>';

$(mystring).filter('div').each(function(e) {
    alert('do something');
});

If you wanted to use your approach, you would need to give the div elements a parent on which jQuery can perform the find.

http://jsfiddle.net/u5uDg/1/

      // Added parent <div> element
var mystring = '<div><div> bleh content </div> <div> bleh content </div></div>';

$('div', mystring).each(function(e) {
  alert('do something');
});

As requested in your comment, you can delay execution of the code in the .each() using setTimeout() 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/

var mystring = '<div> bleh content </div> <div> bleh content </div>';

   // Get the length
var length = $(mystring).filter('div').length;

$(mystring).filter('div').each(function(e) {
    // setTimeout is used to delay code from executing.
    // Here we multiply e (which is the index of the current 
    //   iteration) by 2000 milliseconds, so each iteration
    //   is delayed by an additional 2000ms
    (function(th) {
        setTimeout(function() {
                 alert($(th).text());
                 if(!--length) { alert('done'); } // alert if done
        }, e * 2000);
    }(this));
});​
like image 96
user113716 Avatar answered Oct 17 '22 21:10

user113716


Depending on your intent, the jQuery code will look different.

To iterate through divs with content, the divs need not be assigned to a variable in the javascript. They can just live in the html:

<div class="mystring">bleh content</div>
<div class="mystring">blehhh content</div>

Then, your script will look like the below and you should see the alerts:

$('div.mystring').each(function(e) {
  alert('do something');
});

If you are trying to iterate through a mystring array, your code would look like this:

var mystring = '<div class="mystring">bleh content</div><div class="mystring">blehhh content</div>';
$.each(mystring, function(e) {
  alert('do something');
});
like image 24
frabjousB Avatar answered Oct 17 '22 21:10

frabjousB