[thelist] Trapping JS events

Jeff Howden jeff at jeffhowden.com
Thu Jul 3 03:24:00 CDT 2003


chris,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Chris Evans
> 
> I have a div that contains a form element. Both have
> onMouseDown handlers.  When I click on the form element,
> both handlers get fired, first the form element handler,
> then the containing DIV handler.
>
> [snip]
> 
> When I click on the input box, I get an alert for First
> Event, then one for Second Event.   Is there a way I
> can have the form element event handler trap the mouse
> click so that the containing DIV event handler doesn't
> get called?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

i've had some luck with the following:

function cancelEvent()
{
  if(window.Event)
    return false;
  else
    window.event.cancelBubble = true;
}

<div
 id="myContainer"
 onmousedown="alert('Second Event')">
  <input
   type="text"
   value=""
   id="myFormField"
   onmousedown="alert('First Event'); return cancelEvent()">
</div>

good luck,

.jeff

------------------------------------------------------
Jeff Howden - Web Application Specialist
Resume - http://jeffhowden.com/about/resume/
Code Library - http://evolt.jeffhowden.com/jeff/code/




More information about the thelist mailing list