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 7740677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:50:29+00:00 2026-06-01T08:50:29+00:00

var test = { } $(test).on(testEvent, function (){ console.log(testEvent has fired); }); $.event.trigger(testEvent); I

  • 0
var test = { }
$(test).on("testEvent", function (){
    console.log("testEvent has fired");
});
$.event.trigger("testEvent");

I am trying to using jQuery to do a publish/subscribe mechanism using events. I need to be able to attach events to non-DOM objects and be able to have them all fire from a single global trigger. I expected the code above to work but it did not result in the testEvent firing for the test object.

Note that there will be multiple objects in which an event will be subscribed to. A single $.event.trigger should fire all of those events.

Do note that this code works fine:

$('#someID').on("testEvent", function () {
console.log('testEvent has fired from DOM element');
})
$.event.trigger("testEvent");
  • 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-01T08:50:31+00:00Added an answer on June 1, 2026 at 8:50 am

    After doing some research it appears as though jQuery 1.7 provides an easy way to introduce a publish/subscribe mechanism. (found here) In order to have a publish/subscribe mechanism the following code can be used:

    (function ($, window, undefined) {
        var topics = {};
    
        jQuery.Topic = function (id) {
            var callbacks, method, topic = id && topics[id];
            if (!topic) {
                callbacks = jQuery.Callbacks();
                topic = {
                    publish: callbacks.fire,
                    subscribe: callbacks.add,
                    unsubscribe: callbacks.remove
                };
                if (id) {
                    topics[id] = topic;
                }
            }
            return topic;
        };
    })
    

    In order to subscribe to an event the following is done:

    $.Topic("message").subscribe(function () {
        console.log("a publish has occurred");
    });
    

    In order to publish a message the following is done:

    $.Topic( "message" ).publish(data);
    

    “Message” is the event name. The data argument contains any information you want passed to the subscribers.

    In order to unsubscribe you must pass the function that was subscribed:

    $.Topic( "message" ).unsubscribe(funcSubscribedToBefore);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the working code: var test = function () { console.log(test.data); }; test.data
I am trying to sort a table with JavaScript. function test() { var table
Is there a way to using a JQuery function to programmatically create an event
I'm trying to get the content of the dt element using jquery. var test_name
why does this only alert 1 ? function test() { var myobj = {
Why doesn't this work right? function test() { var start = new Date(2012, 3,
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
var Test = (function() { return { useSub: function () { this.Sub.sayHi(); }, init:
I've been using var.test and bartlett.test to check basic ANOVA assumptions, among others, homoscedascity
I have a function e.g. var test = function () {alert(1);} How can I

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.