JQuery: interacting with the wrapped set – part IV
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.