2022.2

Table Of Contents
prev()
prev()returnstheprevioussiblingofanHTMLelement,whichcanbe:
l
Theelementsthatmatchtheselectorofascript(see"results"onpage844).
l
Oneelementthatmatchestheselectorofascriptthatrunsfor"Eachmatchedelement"(see
"this"onpage781and"Settingthescopeofascript"onpage373).
l
Theelementsreturnedbyaqueryinthetemplate(see"query()"onpage742).
InHTML,siblingsareHTMLelementsthatsharethesameparent,i.e.HTMLelementsinthesamecon-
tainerelement.
Notethatasiblingcanbeadifferenttypeofelement.Forexample,ifaparagraph,animageandatable
followeachotherinthesameBox,theyaresiblings.
Tip: Togetthenextsibling,usenext()(see"next()"onpage802).
Whatiftherearenosiblings?
Ifyoucallprev()ornext()onacollectionofelements(either"results"onpage844ortheresultset
of"query()"onpage742),thefunctionwillreturnacollectionofsiblings.Ifnoneoftheelementshasa
previousornextsibling,thecollectionwillbeempty.Actionsperformedonanemptycollectionwill
simplyhavenoeffect.
Ifyoucallprev()ornext()onasingleelement(thisinan'Eachmatchedelement'script),andthe
elementhasnopreviousornextsibling,thefunctionreturnsnull.Performinganoperationonanull
valuewillresultinanerror,whichmeansthattherestofthescriptwon'tbeexecuted.Inthiscase,itis
usefultocheckthereturnvalue,unlessit'scertainthatitwillneverbenull.
Example
AssumethattherearethreeconsecutiveparagraphsinaBoxandthatthesecondofthoseparagraphs
hasanIDthatmatchestheselectorofthisscript.Theparagraphisstoredintheresultsobject(see"res-
ults"onpage844).Thefollowingscriptchangesthefont-weightofthepreviousparagraph-thefirst
paragraphintheBox-tobold.
results.prev().css("font-weight", "bold");
Iftheelementsinresultsdon'thavesiblings,thislineofcodewillhavenoeffect.Italsowon'tresultin
anerror.
Inan'Eachmatchedelement'scriptyouwouldfirstneedtocheckthereturnvalueofthis.prev():
var prevSibling = this.prev();
if prevSibling != null { prevSibling.css("font-weight", "bold"); }
Page 808