2022.2

Table Of Contents
Selector Paragraph
before script
execution
Paragraph after script
execution
span <p>Loremipsum<span>-
dolorsit</span>amet,con-
sectetueradipiscingelit.</p>
<p>Loremipsum<span></span>amet,
consectetueradipiscingelit.</p>
filter()
filter(callback)
Returnsasubsetofaset.Allelementsforwhichthecallbackfunctionreturnstruewillbeincludedin
theresult.
callback
Afunctionusedasatestforeachelementintheset.Filter()passestheiterationindexandthecurrent
elementtothecallbackfunction.Inthescopeofthecallbackfunction,thisreferstothecurrentele-
ment.
Example
Theselectorofthefollowingscriptisli(listitem),sotheresultsobjectcontainsalllistitemsinthe
template.Thescriptsfiltersthethirdandsixthlineitemsfromtheresults,takingadvantageofthe
indexthatispassedtothefilterfunction,andcolorsthemred.Itusesthemodulusoperator(%)to
selecteveryitemwithanindexvaluethat,whendividedby3,hasaremainderof2.(Theindexstarts
countingatzero.)
results.filter(function(index) {
return index % 3 === 2;
}).css( "background-color", "red" );
filter(selector)
Returnsasubsetofaset.Allelementsmatchingtheselectorwillbeincludedintheresult.
Thedifferencebetweenresults.filter(selector)andquery(selector, results)isthat
query()searchesthroughouttheentireresultswhilefilter()onlytakesthetop-levelelementsinto
account.
selector
Page 1220