Solved

useCapture Javascript - explanation

Posted on 2009-04-03
2
692 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
Comment Utility
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
Comment Utility
Hi niko86,

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

vkimura
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now