0

How can I add on event listener for custom object.

For example:

(function () {
    this.EmojiPicker = (function () {
        function EmojiPicker(options) {
            // ...
        }

        EmojiPicker.prototype.test = function () {
            //...
        };

        return EmojiPicker;

    })();
}).call(this);

I want to add

let emojiPicker = new EmojiPicker();

emojiPicker.on("change", function () {
    //
});
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144

1 Answers1

2

I have a simple example modified from you code. See if this can help.

The main idea is to trigger an event by $(emojiPicker).trigger('change');, so that you can receive with $(emojiPicker).on("change", function () {});

<html>
<head>
    <script type="text/javascript" src="./jquery-1.7.1.js"></script>
    <script>
    (function () {
        
        this.EmojiPicker = (function () {
            function EmojiPicker(options) {
                // ...
            }

            EmojiPicker.prototype.test = function () {
                //...
                $(emojiPicker).trigger('change');
            };

            return EmojiPicker;

        })();
        
        
        let emojiPicker = new EmojiPicker();

        $(emojiPicker).on("change", function () {
            alert("triggered");
        });
    }).call(this);
        
    </script>
</head>
<body>
</bofy>
</html>

More information from Custom events in jQuery?

ian00
  • 134
  • 1
  • 4