2022.2

Table Of Contents
Thisnarrowsthescopeofthesearchandresultsinaveryfastquery,aselementswithanIDorclass
areindexedbyConnectDesigner'slayoutengine.
ForinformationabouthowtogiveanelementanIDorclass,see"IDandclass"onpage172.
Seealso:"Quick-startascriptforaspecificIDorclass"onpage371.
Targeting text
TextinitselfcannothaveanIDorclass,buttheelementthatcontainsitcan.Thesmallestpossiblecon-
taineroftextisaSpan.TolearnhowtoputtextinsideaSpan,see"Span"onpage229.GivetheSpan
anIDorclassandusethatasthescript'sselector.
Totargettextinabiggercontainer(aparagraph,forexample),changetheFindmethodofthescriptto
Selector and Text,usetheIDorclassofthecontainerelementasSelectorandtypethetextinthe
Textfield.
Tip: DraggingadatafielddirectlytotheScriptspanecreatesascriptwithaclassselector,
withoutaddingaplaceholdertothetemplate.
Tip: Whenyouusethedrag-and-dropmethod(withoutpressingtheALTkey)toinsertvariable
dataplaceholdersintoatemplate,thescript'sselectorisbydefaulttheclassoftheelement-a
SpanorDiv-inwhichtheplaceholderiswrapped.Formoreinformationsee:"Variabledatain
text:scriptsandplaceholders"onpage311.
AvoidDOMmanipulations
TheScriptingAPIoftheDesignerisaverypowerfultooltomanipulateandpersonalizeyourdocument.
ButkeepinmindthatDOMmanipulationcommandslikeappend(),prepend(),before()andafter()are
resourceintensive.
TryavoidingDOMmodifications,especiallywithinloops.Storingthecontentinavariableandappend-
ingtheinformationaftertheloopismoreefficient:thisway,thetemplatewillbetouchedonlyonce.
Example: Thefollowingexampleloadsasnippetintoavariableandusesthefind()andtext()
commandsoftheDesignerscriptingAPI.
var labelElm = loadhtml('snippets/label.html');
for(var i = 0; i < record.tables.products.length; i++) {
var label = labelElm.clone();
label.find('@ProductLabel@').text(record.tables.products[i].ProductDescription);
results.after(label);
}
What'swrongwiththiscodeisthatitinsertsthepersonalizedinformationwithintheloop.Theafter()
commandrunsasmanytimesastherearerecordsinthedetailtable'products'.
Page 381