Skip to content

06. Add Remove Click Events (pARCE.js)

Pimp Trizkit edited this page Mar 6, 2018 · 1 revision
<< Previous        Back to Table of Contents        Next >>

    "You want in my clique?" - PT

Dependency: pDA (disableable)

This will add or remove a click callback associated with an element. It also allows for use of the optional once attribute that will automatically remove the event listener from the element once it fires the first time, therefore, it only fires "once".

pARCE will also use pDA to cancel out any highlighting caused from accidental double clicking by adding it as a dblclick listener. Remove e.addEventListener("dblclick",pDA); to disable this feature.

Code:

const pARCE=(a,e,c,o)=>{if(a){e.addEventListener("click",c,(o?{once:true}:false));e.addEventListener("dblclick",pDA);}else e.removeEventListener("click",c);}

Usage:

pARCE(true,myElementA,myCallbackFuncA,true); // Add click event, single use (automatic removal).
pARCE(true,myElementB,myCallbackFuncB,false); // Add click event, multi use.
pARCE(true,myElementB,myCallbackFuncB); // Add click event, multi use, default setting.
pARCE(false,myElementB,myCallbackFuncB); // Manual remove click event

Return:

Nothing, get used to it. But your thing works now.

Params:

pARCE(a,e,c,o)

a = < Add/Remove Boolean > * REQUIRED

  • A true or false equivalent boolean used to add or remove the click event, respectively. Suggested to use true or false.

e = < HTML Element > * REQUIRED

  • The element to which to add or remove the click event.
  • If a above is set to true then pDA will also be added as an event listener for dblclick (double clicks) on this element.

c = < Callback Function > * REQUIRED

  • The callback function to which to attach to or remove from the click event on the specifed element (e above).

o = < Once/Multi Boolean > Optional

  • A true or false equivalent boolean used to set the once attribute. Suggested to use true or false.
  • The once attribute will allow the event listener to run only once, then the browswer will remove it from the element automatically.
  • If omitted, pARCE will default to a multi-use event listener.
  • Skipped if a is false.

Clone this wiki locally