2022.2

Table Of Contents
elementdirectlyviathethisobject.See"Settingthescopeofascript"onpage373and"this"
onpage781.
each(callback)
Iteratesovertheelementsinaset,suchastheenumerablepropertiesofanobject,inarbitraryorder.
Foreachdistinctproperty,statementscanbeexecuted.
callback
Afunction.Thecallbackfunctionispassedtheiterationindexandthecurrentelement.Inthescopeof
thecallbackfunction,thisreferstothecurrentelement.
Examples
Thefollowingscriptsdemonstrateasimpleiterationovertheelementsintheresults(thesetof
HTMLelementsthatmatchtheselectorofthescript).
Thefirstscriptsetsthebackgroundcolorofeachoftheelementstored.(Thisisjusttodemonstrate
howthisfunctionworks.ItiseasiertochangethestyleofasetofHTMLelementsusingthecss()func-
tion;see"css()"onpage796.)
results.each(function(index){
results[index].css('background-color','red');
});
Thisscriptaddsarandomintegertoeachelementintheresultset.
results.each(function(index){
var test = Math.floor((Math.random() * 10) + 1);
this.html(test);
});
Selector Matched ele-
ment
Matched element after script exe-
cution
p <p></p>
<p></p>
<p></p>
<p>3</p>
<p>1</p>
<p>7</p>
Thenextscriptgetstherowindex(ofthecurrentelementintheset)andputsitinaparagraph.
results.each(function(index){
this.text(index);
}
Page 717