Inordertodothis, weneedtocreate a newfileinourprojectandwe'lljustcallitstoredotJs, whichstandsfor a job, a skirtfile.
SothedotJsextensiontellsusthatthisisgoingtobe a filewithjobdescriptionandtheninordertoincludethatfileinsteadofourstoredothtml, weneedtogointoherheadandinclude a scripttaghereandgiveit a sourceAttributesandthissourceattributeworksexactlythesameas a troughattributesforpathoreuro.
Butit's alwayssuggestedtouse a job's gonnafile, asopposedtoJavascriptinsideof a scripttagsimilartousing a styleelementasopposedtoanexternalCSSstylesheet.
Inordertogetaroundthisandmakeourscripthagloadinthebackground, weneedtoaddthe A sinkattributehere, whichtellsourbrowserinordertodownloadthisstoredotjazzpageinthebackgroundandcontinuedownloadingtheactualbodyofourHTMLpageatthesametime.
We'llcreate a variableherethatwillbetheactualbutton.
Sowhat's calledwell, saiditequaltowhicheverelementintheloopthatwe'rein, because I willconstantlybehavingoneaddedtoiteachtimewegothroughthisalready.
Sowe'regoingtoselecttheotherelementofthisarray.
Setittothisbuttonelementhere.
Sonowwecanusebutton.
Andsincethisbuttoncorrespondstooneofourremovebuttonsoverontherighthere, allwehavetodoissay, buttondotadeventlistener, andthisishowweadd a listenertotelluswhenweclickon a button, dosomething.
Soinourcase, we'regoingtolistenfortheclickevent, andwhentheclickeventhappens, wewanttorunsomecode, sowe'llcreate a functionforthat.
Andfornow, we'rejustgonnawalktothecouncil.
I wouldn't sayclicked, andifwesavethat, evergoclick.
Wouldjustsaythatthisisourcartroseandnowallweneedtodoisloopoverallofthesedifferentcartroseverysimilarlytohowwelookedoverallthebuttonsuphere, so I'lljustcopythiscode.
Hey, sitdownhereandchangethistobecartroast.
Sincewewanttoloopoverourcart, Roseandthenwe'lljustsaycredited a variablehere.
Andthen, sincewewantthistobe a numberandnot a string, weneedtousetheparsefloatmethod, whichwillturnanystringintowafloat, whichisessentially a numberwithdecimalpointsafterit.
Therewego.
NowifwesaythatanddothisremoveagainObviouslywenowhave 9 99 as a numberwithoutthedollarsign, whichisexactlywhatweneedinordertodomathonthisnumber.
Andsincethisisinsideof a loopweneed a totalallofthesetogether.
Sobefore I leave, let's create a variablewillcall a totalandwe'regoingtosetthistozerotostartwithandthenwewanttoaddSototalisgoingtoequaltotalplusthepricetimesthequantityanditwilldothateverytimeitgoesthroughloop.
Now, beforewegofurtherwithactuallycreatingtherestofourbuttons, I dowannamakenoteofonethingthatisparticularwhichof a scriptthatweneedtomakesureofbeforeweactuallystarttryingtoaccessthedifferentelementsonourpage.
Weneedtomakesurethatourpageisdoneloading.
Ifyourememberwhenweaddedthisscripttag, weaddedthis a sinkmethodhere, whichtellsitthatitwillloadinthebackgroundwhilethebodyofourelementisloading.
Andyouhavetomakesurethatyouhavethecapitalizationcorrectforallofthesedifferentlettersinorderforthistowork, andthiseventwillfireassoonasthepagesdownloadingsoinsideofherewejustwanttorun a function.
Whenthecodegetstothispoint, anotherthingwecandoiswecancleanupourcode a littlebitbycreating a function, andwe'lljustcallthisfunctionherewillcallitremoveCARTitem, andthisfunctionwilltakeallofthecode, thatis, insteadofoureventlistenerhereforremoving a cartbutton.
Because, asyousee, if I increasethisnumberaretotalstaysthesame, andwecanalsoinputnumbersbolosbelowzero, whichwedonotwanttobeabletodobecausewedon't wantsomeonetoordernegative 14 T shirts.
Sowhatwe'regonnadoiswe'regonnadosomethingverysimilartowhatwedowithourcardbuttonshere, butwe'regoingtodoitfor a quantityinput.
We'regonnasetuptoeventdottargetsinceweknowthatthetargetofoureventisgoingtobetheactualinputelementthatweneed, andthenwewanttochecktoseeifthevalueinsideofthisinputis a validvalue.
Sothefirstthingwewanttocheckforiswe'regonnacheckforifit's actuallynumber, becauseifthepersondeletesitandclicksoff, thisisnolongernumber, sowecanusethefunctionis N A N, whichdanceforisnot a number, andwecanpassinourinputthatvalueinhere, andthiswillchecktoseeifourinputis a numberorifit's not a number, andwealsowanttomakesurethatournumberisnot a negativenumber.
Youcan't order a zeroornegativeoneofanythingsowecanchecktoseeifit's not a numberoriftheinputvalueislessthanorequaltozero, whichmeansthattheydidputzeronegativeonenegativetoanythinglessthanoneinsideofherewewanttodoiswewanttosetourquantity, ourinputvalue.
Wewanttoset a toeone, sinceoneisthelowestpossiblenumberwewantsomebodytobeabletopurchaseanitemofandthatallweneedtodoafterthatisupdatethetotalinsideofourcart.
Butsincewewantaroundtotwodecimalplaceswillfirstmultiplyourtotalby 100 whichwillmoveourtwonumbersafter a desperateplaceinfrontofthedecimalplacearoundit.
Soit's a button, thatadeventlistenerwewanttodoclickagain, sincewewanttodosomethingwhenweclickonthebutton, andthenweneedtogiveit a functionnameandwe'rejustgoingtocreate a functioncalledAddtoCartclickedandnevergo.
It'lltakethateventparameter, andthenwecandoagaincreatethisactualbuttonssowe'llsaythebuttonhereisgoingtobeequaltoeventoptarget, andthisisouraddtocartbuttonnow, sonow, ifwescrolldownto a cart, wecanseethatthereis a fewelementsweneedtoaddtoourcart.
Weneedtoaddtheimage.
Weneed a nameoftheitem, thepriceoftheitemandthenthequantityinputaswellastheremovalbutton.
Andit's goingtobedifferentforallofourcurrentimages, asyoucansee, Sonowwe'regoingtodoisweneedtoactuallyadd a rowtoeourcartdownhereandthisisgoingtobethemostcomplexpartofwhatwe'regoingtodo, So I'm goingtocreate a separatemethodtodothis.
I willcallthistheaditemtoCARTmethodandwe'regonnapassinthetitle.
Soifwerunthisdocumentdotcreateomen, it's goingtocreate a brandnewdayifit's notgonnaaddittoourage, Timoyet.
Butwehave a diffthatwecannowlateraddtoourhtmlandwewanttoaddittoourcartitemsasyoucanseeherewescrolldown, wehavecarteitems, whichiswhereallofourcartrosearegoingtobe.
Sowewantedjustalerttheuserwiththealertfunctionandtheyweren't functionwillpopup a popupfortheuser, tellingthemthatsomethinghashappenedandtheycanclickokaytodismissthepopup.
Sonowifwesavethatwehad a coffeecup, you'llseeitaddsitperfectlydownhere.
Ifwetrytoaddanothercoffeecup, we'regonnaget a messagesayingthattheitemhasalreadybeenaddedthecartandyounoticethatdoesnotaddanotheronetothecart, whichisperfect.
Youwillnotice, however, thatourtotaldownhereisnotbeingupdated.
Soinouraddtocart, click a buttonoffenserighthere.
WhathasputourupdatecarTotalfunctionrightthere.
Andnowwhenwehad a coffeecup, younoticethatourtotalupdatecorrespondinglywithourcoffeecup.
Weknowthere's onlyone, sowe'rejustgoingtogetthe 1st 1 andthenwe'regonnawant a loopoveralltheChildreninsideofthecar.
Soifweadd a bunchofthingsinthecar, youseethatwe'regonnahavemultiplecartroseinhere, sowewanttocheckforalltheChildreninsideofthiscartitems, sowe'llsay, whilewhichissimilarto a fourloop.