Solved

useCapture Javascript - explanation

Posted on 2009-04-03
2
693 Views
Last Modified: 2012-05-06
Hi,

I'm just wondering if somone can explain the use of useCapture in Javascript and give a simple example:

useCapture: Boolean indicating whether to bind the event as it is propogating towards the target node, (event Capture), or as the event bubbles upwards from the target (event bubble). Set to true or false, respectively.
(ref. http://www.javascriptkit.com/domref/elementmethods.shtml)

I'm not certain what is means when it states "to bind the event as it is propogating towards the target node". What is propogating? Can you give a simple example.

And the statement "as the event bubbles upwards from the target (event bubble)". What does this mean? What does it mean by "event bubbles upwards"? Please a small example.

I find examples help with understanding and clarity. So it would be good to just illustrate it with a simple example.

Thank you very much,
vkimura


0
Comment
Question by:Victor Kimura
2 Comments
 
LVL 5

Accepted Solution

by:
niko86 earned 500 total points
ID: 24066014
the mozilla dom reference states:
If true, useCapture indicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listener before being dispatched to any EventTargets beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Events for a detailed explanation.

more info:
http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-flow

in short terms, useCapture prevents events from being triggered during the target and bubbling phases.
it will only trigger in the capture phase
<html>

 <head>

   <title>DOM Event Example</title>

   <style type="text/css">

     #t { border: 1px solid red }

     #t1 { background-color: pink; }

   </style>

   <script type="text/javascript">
 

   // Function to change the content of t2

   function modifyText() {

     var t2 = document.getElementById("t2");

     t2.firstChild.nodeValue = "three";    

   }

 

   // Function to add event listener to t

   function load() { 

     var el = document.getElementById("t"); 

     el.addEventListener("click", modifyText, false); 

   } 
 

   </script> 

 </head> 

 <body onload="load();"> 

 <table id="t"> 

   <tr><td id="t1">one</td></tr> 

   <tr><td id="t2">two</td></tr> 

 </table> 

 </body> 

 </html> 

Open in new window

0
 

Author Closing Comment

by:Victor Kimura
ID: 31566516
Hi niko86,

Okay, I think I'm beginning to understand. Thank you.

vkimura
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now