Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:18:07+00:00 2026-06-05T13:18:07+00:00

What I want to have is a custom object which provides some events. For

  • 0

What I want to have is a custom object which provides some events.
For example:

var CustomObjectTextChangedEventName = 'textChanged';
var CustomObject = function () {
    var _this = this;
    var _text = "";

    _this.OnTextChanged = document.createEvent("Event");
    _this.OnTextChanged.initEvent(CustomObjectTextChangedEventName, true, false);

    _this.ChangeText = function (newText) {
        _text = newText;
        fireTextChanged();
    };

    function fireTextChanged() {
        _this.dispatchEvent(_this.OnTextChanged);
    }
}

The code to use the event would look like:

myCustomObject = new CustomObject();
myCustomObject.addEventListener(CustomObjectTextChangedEventName, handleTextChanged, false);

As you can see… the default way of using events in JS, but I can not make it work…

Currently my problem is that my object does not implement addEventListener and dispatchEvent, but this functions are normally implemented from "element"…

Can I make them available somehow or do I have to implement them for my own?
How I have to implement them?

Do I have to implement my own event handling? (having a internal list of handlers, an "add"- and "remove"-handler function, and fire each handler when I want to fire the event)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:18:11+00:00Added an answer on June 5, 2026 at 1:18 pm

    The addEventListener function is a method of Element class. One way is to make CustomObject inherit from Element like this:

    CustomObject.prototype = Element.prototype;
    

    The problem is that Element class may have different implementations among different browsers. So for example firing events may not be easy (see this post).

    So I advice doing this by yourself. It is not difficult, try something like this:

    var CustomObject = function () {
        var _this = this;
        _this.events = {};
    
        _this.addEventListener = function(name, handler) {
            if (_this.events.hasOwnProperty(name))
                _this.events[name].push(handler);
            else
                _this.events[name] = [handler];
        };
    
        _this.removeEventListener = function(name, handler) {
            /* This is a bit tricky, because how would you identify functions?
               This simple solution should work if you pass THE SAME handler. */
            if (!_this.events.hasOwnProperty(name))
                return;
    
            var index = _this.events[name].indexOf(handler);
            if (index != -1)
                _this.events[name].splice(index, 1);
        };
    
        _this.fireEvent = function(name, args) {
            if (!_this.events.hasOwnProperty(name))
                return;
    
            if (!args || !args.length)
                args = [];
    
            var evs = _this.events[name], l = evs.length;
            for (var i = 0; i < l; i++) {
                evs[i].apply(null, args);
            }
        };
    }
    

    Now using it is as simple as:

    var co = new CustomObject();
    co.addEventListener('textChange', function(name) {
        console.log(name); 
    });
    co.fireEvent('textChange', ['test']);
    

    This is a basic solution. You may want to alter it, but I think you should grasp the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom principal object that I want to be able to serialize
I have a QPushButton, QDateEdit and another custom object. I want to connect the
I have a custom class which I want to load inside the firstViewController and
I have a custom object in JavaScript: function Graph (dataType, provider){ this.dataType = dataType;
For some reason, I want to return an object of my::Vector (which is basically
I have a custom object with several properties, one of which returns a list.
I have an array of a custom JavaScript object which has a property named
We have a Salesforce app where we have some custom objects and want to
I have an array of custom objects. MyCustomArr[]. I want to convert this to
I have a listView with custom objects defined by the xml-layout below. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.