zjrJS Javascript Library

zjrJS.Doc.eP() - zjrJS Javascript Library

This information lives on a webpage hosted at the following web address: 'https://omegajunior.globat.com/code/zjrjs/'.

How to use zjrJS.Doc.eP() ("prevent default action on events")

Declaration
zjrJS.Doc.eP(Event evt)


Goal
A cross-browser method to prevent the default action from occurring on event, and cancel bubbling and propagation. It negates the difference between standards compliant and stubborn browsers, with a fallback for DOM-free browsers.

Typical usage
When you assign collection events on element parents, after handling an event you wish to prevent the parent from executing the event as well. The event normally travels up through the DOM tree, in a process called event propagation. The associated function happens for each element in the DOM hierarchy up to the element that registered the event listener. eP() cancels that propagation, so the associated function happens only once per event.

Member Type
Function.

Value Type
Boolean (false).

Parameters
Event evt: the global or specific event automatically created by the browser's event system.

Dependencies
None.

Example
<script type="text/javascript" src="zjrJS.version.js" id="zjrJS"></script>
<script type="text/javascript">/* <![CDATA[ */

//define a function to alert the id of a clicked button
function sayHello(evt){
alert(zjrJS.Doc.eS(evt).id);
//cancel event propagation and defualt actions from taking place,
//so myButtonList won't say hello.
zjrJS.Doc.eP(evt);
}

//tell all buttons in a container to say hello
zjrJS.Doc.aE(zjrJS.Doc.eid("myButtonList"), "click", sayHello);

/* ]]> */</script>


Notice that this way, only 1 event handler is defined and added. That could save quite a bit of memory. Since by default the events propagate upwards through the node hierarchy, clicking a button will result in clicking its container, which results in the desired action. In the above example, eP() is used to stop the event propagation, preventing the button container "myButtonList" from saying hello itself.

Download:

Download zjrJS’ latest stable build (20141202t2322) minified (7.1KB).

Download zjrJS’ latest stable build (20141202t2322) (11.9KB).

Clicky Analytics