0
function datePicker(event)
{
//do something`enter code here`
}

document.onclick=function() {
//do something
}

When I call datePicker() function I want to prevent the calling second method . How to do that ?

SKG
  • 510
  • 4
  • 20

1 Answers1

3

Use event.stopPropagation() to stop the event bubbling up to the document click listener:

function datePicker(event)
{
    event.stopPropagation();
}

You can also use event.stopImmediatePropagation() to stop sibling elements receiving the event too, if necessary: jquery: stopPropagation vs stopImmediatePropagation

jQuery Example with IE8 catered for: http://jsfiddle.net/kHT2A/2/

Community
  • 1
  • 1
Joe
  • 15,669
  • 4
  • 48
  • 83
  • Is the function `datePicker` actually receiving an event, or just a parameter called event? – Joe Feb 23 '12 at 13:46
  • 1
    `stopPropogation` isn't available in IE8 and lower, and `stopImmediatePropagation` isn't available natively in any browser. –  Feb 23 '12 at 13:47
  • Just found out it's prop/a/gation, not prop/o/gation - you learn something new every day :) Updated answer with the correct spelling Also, see http://jsfiddle.net/kHT2A/1/ for a working example with jQuery – Joe Feb 23 '12 at 13:49