Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

alexandis commented Nov 12, 2015

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
You can’t perform that action at this time.