Skip to content

Instantly share code, notes, and snippets.

@Kcko Kcko/demo.html
Last active Apr 22, 2018

What would you like to do?
What about using triggers? Does anyone know any drawback using them? The benefit is that all internal variables are accessible via the triggers, and the code is very simple.
See on jsfiddle.
Example usage
<div id="mydiv">This is the message container...</div>
var mp = $("#mydiv").messagePlugin();
// the plugin returns the element it is called on
mp.trigger("messagePlugin.saySomething", "hello");
// so defining the mp variable is not needed...
jQuery.fn.messagePlugin = function() {
return this.each(function() {
var lastmessage,
$this = $(this);
$this.on('messagePlugin.saySomething', function(e, message) {
lastmessage = message;
$this.on('messagePlugin.repeatLastMessage', function(e) {
function saySomething(message) {
$this.html("<p>" + message + "</p>");
function repeatLastMessage() {
$this.append('<p>Last message was: ' + lastmessage + '</p>');
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.