[jQuery] Re: loop through elements and stop at first match
Thanks for your answers. An ordinary loop is what I need. The reason why I need this is because I want the extend Jake's textNodes plugin in order to highlight/animate a word made out of letters from a text. For instance for the word selector: Lorem ipsum dolor *s*it amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laore*e*t dolore magna aliquam erat vo*l*utpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip *e*x ea commodo *c*onsequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequa*t*, vel illum dolore eu feugiat nulla facilisis at vero er*o*s et accumsan et iusto odio dignissim qui blandit p*r*aesent luptatum zzril delenit a I think I need a lot of loops in order to choose letters not too close to eachother, etc Thanks for showing me the right direction Olivier Michael Geary wrote: As Rob suggested, explore what you can do with the selectors available in jQuery. But if those don't do the trick, keep in mind that the jQuery result object is an array. If you want to do something unusual with it that isn't provided by the jQuery selectors, you can access the array elements directly with an ordinary loop or any other code you want to write. This typical jQuery code: // Iterate through all elements with class foo $('.foo').each( function() { var element = this; // do something with element } is essentially the same as: // Iterate through all elements with class foo var $foo = $('.foo'); for( var i = 0, n = $foo.length; i n; ++i ) { var element = $foo[i]; // do something with element } Once you access the array elements directly like this, you have the flexibility to do whatever you want in your code. As a silly example: // Iterate through the middle third of the // foo elements, in reverse order var $foo = $('.foo'), n = $foo.length; var first = n / 3, last = n * 2 / 3; for( var i = last - 1; i = first; i-- ) { var element = $foo[i]; // do something with element } -Mike *From:* Olivier Percebois-Garve I want to loop through the jquery array of objects, stop to loop when it finds the first match, and then continue to loop with another search. In another language I would set a var found = false; before the loop and then set it to true in the loop, but with chaining I'm not sure how to do. Any idea ? Olivier
[jQuery] Re: loop through elements and stop at first match
jQuery's in-built selectors with the custom selector ability should provide you with everything you need without having to loop through the array yourself. What are you trying to search for? On 7/9/07, Olivier Percebois-Garve [EMAIL PROTECTED] wrote: Hi I want to loop through the jquery array of objects, stop to loop when it finds the first match, and then continue to loop with another search. In another language I would set a var found = false; before the loop and then set it to true in the loop, but with chaining I'm not sure how to do. Any idea ? Olivier -- Rob Desbois Eml: [EMAIL PROTECTED] Tel: 01452 760631 Mob: 07946 705987 There's a whale there's a whale there's a whale fish he cried, and the whale was in full view. ...Then ooh welcome. Ahhh. Ooh mug welcome.
[jQuery] Re: loop through elements and stop at first match
And what's the point of stopping the loop only to continue it again? _ From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Rob Desbois Sent: Monday, July 09, 2007 8:57 AM To: jquery-en@googlegroups.com Subject: [jQuery] Re: loop through elements and stop at first match jQuery's in-built selectors with the custom selector ability should provide you with everything you need without having to loop through the array yourself. What are you trying to search for? On 7/9/07, Olivier Percebois-Garve [EMAIL PROTECTED] wrote: Hi I want to loop through the jquery array of objects, stop to loop when it finds the first match, and then continue to loop with another search. In another language I would set a var found = false; before the loop and then set it to true in the loop, but with chaining I'm not sure how to do. Any idea ? Olivier -- Rob Desbois Eml: [EMAIL PROTECTED] Tel: 01452 760631 Mob: 07946 705987 There's a whale there's a whale there's a whale fish he cried, and the whale was in full view. ...Then ooh welcome. Ahhh. Ooh mug welcome.
[jQuery] Re: loop through elements and stop at first match
As Rob suggested, explore what you can do with the selectors available in jQuery. But if those don't do the trick, keep in mind that the jQuery result object is an array. If you want to do something unusual with it that isn't provided by the jQuery selectors, you can access the array elements directly with an ordinary loop or any other code you want to write. This typical jQuery code: // Iterate through all elements with class foo $('.foo').each( function() { var element = this; // do something with element } is essentially the same as: // Iterate through all elements with class foo var $foo = $('.foo'); for( var i = 0, n = $foo.length; i n; ++i ) { var element = $foo[i]; // do something with element } Once you access the array elements directly like this, you have the flexibility to do whatever you want in your code. As a silly example: // Iterate through the middle third of the // foo elements, in reverse order var $foo = $('.foo'), n = $foo.length; var first = n / 3, last = n * 2 / 3; for( var i = last - 1; i = first; i-- ) { var element = $foo[i]; // do something with element } -Mike _ From: Olivier Percebois-Garve I want to loop through the jquery array of objects, stop to loop when it finds the first match, and then continue to loop with another search. In another language I would set a var found = false; before the loop and then set it to true in the loop, but with chaining I'm not sure how to do. Any idea ? Olivier