2022.2

Table Of Contents
Addinganeventhandler
Firstofallyouneedtowriteaneventhandlerthatrespondstotheeventthatismeanttotriggerthecre-
ationofthewidget(e.g.theonClickeventofabuttonorlink),bycallingthefunctionthatcreatesthewid-
get.Theeventhandlerhastobeaddedonthe$(document).ready()function,whichisfiredwhenthe
Formisloadedinthebrowser/app.
$(document).ready(function() {
$('#add-element').on('click', function() {
//call the function that creates the widget
});
});
Creatingthewidget
Nowyoucanstartwritingthecodethatconstructs,addsandinitializesthewidget.Thiscodehastobe
basedonjQuery.
Constructing the HTML
AwidgetbasicallyisanHTMLelementwithcertainattributesandcontents.TheHTMLstructureofa
widgetcanbeseenontheSourcetabafteraddingthewidgettoaFormintheDesigner.Incode,recon-
structtheHTML.MakesuretogivethenewelementanID.
ThiscodeconstructstheHTMLofaDateelement:
<label>date1
<input id="date1" role="cotg.DatePicker" readonly="" name="date1" type="text">
</label>
Adding it to the Form
AddtheHTMLtoanelementontheFormusingtheappend()function.
ThefollowingcodeappendsthecontentsofthevariablehtmltoanelementontheFormthathastheID
cameras:
$('#cameras').append(html);.
Initializing the widget
Awidgethastobeinitializedtoallowittobeactuallyused.WidgetsthatarealreadypresentinaForm
atstartupareinitializedassoonastheFormisloadedintheapp.Awidgetthathasbeenaddedtoa
FormdynamicallyhastobeinitializeddirectlyafteraddingittotheForm;otherwisethenewwidget
won'trespondtoactionsoftheuser,eventhoughitisvisibleintheapp.
Initializingawidgettakesjustonelineofcode,inwhichyouselectthenewwidgetbyitsIDandcallthe
initializationfunctiononit.Thiscode,forexample,initializesanewCameraelementthathastheID
myCamera:
Page 549