Skip to content

Instantly share code, notes, and snippets.

@oslego
Created December 11, 2010 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oslego/737611 to your computer and use it in GitHub Desktop.
Save oslego/737611 to your computer and use it in GitHub Desktop.
Custom jQuery events triggered on non-DOM elements that bubble (propagate) up to the 'document'
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
(function($){
//non-DOM object events that propagate up to the 'document'
$(document).bind('customEventName', function(e, payload){
console.log(payload.content);
})
// non-DOM object
var obj = {};
obj.ownerDocument = document; //magic happens here
$(obj).trigger("customEventName", {content: "Witchcraft"})
})(jQuery)
</script>
</body>
</html>
@alexandis
Copy link

Tried this. Does not work. I have to use $(document) for trigger instead...

http://jsfiddle.net/f8vw6ebk/

Anyway, bubble does not happen. Have to use some other approach...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment