JQuery: interacting with the wrapped set – part IV

Published Sat, Jul 18 2009 18:15

In this short post (sorry, the weather is looking good here and I’m still on vacations :)) we’re going to talk about a single method: the each method.  The each method expects a reference to a function (this method has only a single parameter) and it will invoke that method for each instance of the wrapped set.

Inside the function, you’re supposed to use the this variable to access the current element. Notice that if you return false from the method, you’re actually stopping the interaction. Here’s a quick example of how you can use this method:

var exists = false;
$("div").each(
function() {
if (this.innerText === "luis") {
  exists = true;
  return false;
}
});

As you can see, we’re using a function to see if any of the existing divs contains the text luis. Notice that there are more elegant ways of achieving this kind of testing. However, if you wanted to, say, use some sort of regular expression for checking the existence of a value, then this would be a good way of doing that.

And that’s all for today. Keep tuned for more on JQuery.

Filed under:

Comments

# 9eFish said on Sunday, July 19, 2009 9:09 AM

9efish.感谢你的文章 - Trackback from 9eFish

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover