Search code examples
javascriptfunctiononclickstoppropagation

How is this function parameter undefined?


I'm trying to replicate this 'onclickout simulator' script as seen here:

http://jsfiddle.net/C9CL3/

As you can see, it works fine (in all of the browsers I've tested it in - so this isn't a browser compatibility issue as far as I'm aware).

The code in my solution reads as follows:

document.getElementById('bigBtn').onclick = function(event) {
        alert('button clicked');
        if (event.cancelBubble) //THIS LINE GIVES 'EVENT IS UNDEFINED' ERROR
            event.cancelBubble = true;
        else
            event.stopPropagation();
    }

This is found in pageLoad().

As you can see, I'm getting an unexpected error from the event.cancelBubble line as event is undefined. I can't for the life of me work out why.

NOTE: I am testing cancelBubble in this way differently from the fiddle demo for browser compatibility reasons.

Thanks a lot for any help, really stuck on this.


Solution

  • Problem solved. Instead of 'simulating' an onclickout event in this way, I simply set focus (using window.hash) to the menu that was to appear in place of that 'button clicked' alert and then handled the onblur event to close it.