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

  • Home
  • SEARCH
  • 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 100133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:31:16+00:00 2026-05-11T00:31:16+00:00

I’m looking for some input on how to implement custom eventhandling in jquery the

  • 0

I’m looking for some input on how to implement custom eventhandling in jquery the best way. I know how to hook up events from the dom elements like ‘click’ etc, but I’m building a tiny javascript library/plugin to handle some preview functionality.

I’ve got a script running to update some text in a dom element from a set of rules and data/user input I got, but now I need that same text shown in other elements that this script can’t possibly know of. What I need is a good pattern to somehow observe this script producing the needed text.

So how do I do this? Did I overlook some builtin functionality in jquery to raise/handle user events or do I need some jquery plugin to do it? What do you think is the best way/plugin to handle this?

  • 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. 2026-05-11T00:31:17+00:00Added an answer on May 11, 2026 at 12:31 am

    Take a look at this:

    (reprinted from the expired blog page http://jamiethompson.co.uk/web/2008/06/17/publish-subscribe-with-jquery/ based on the archived version at http://web.archive.org/web/20130120010146/http://jamiethompson.co.uk/web/2008/06/17/publish-subscribe-with-jquery/)


    Publish / Subscribe With jQuery

    June 17th, 2008

    With a view to writing a jQuery UI integrated with the offline functionality of Google Gears i’ve been toying with some code to poll for network connection status using jQuery.

    The Network Detection Object

    The basic premise is very simple. We create an instance of a network detection object which will poll a URL at regular intervals. Should these HTTP requests fail we can assume that network connectivity has been lost, or the server is simply unreachable at the current time.

    $.networkDetection = function(url,interval){     var url = url;     var interval = interval;     online = false;     this.StartPolling = function(){         this.StopPolling();         this.timer = setInterval(poll, interval);     };     this.StopPolling = function(){         clearInterval(this.timer);     };     this.setPollInterval= function(i) {         interval = i;     };     this.getOnlineStatus = function(){         return online;     };     function poll() {         $.ajax({             type: 'POST',             url: url,             dataType: 'text',             error: function(){                 online = false;                 $(document).trigger('status.networkDetection',[false]);             },             success: function(){                 online = true;                 $(document).trigger('status.networkDetection',[true]);             }         });     }; }; 

    You can view the demo here. Set your browser to work offline and see what happens…. no, it’s not very exciting.

    Trigger and Bind

    What is exciting though (or at least what is exciting me) is the method by which the status gets relayed through the application. I’ve stumbled upon a largely un-discussed method of implementing a pub/sub system using jQuery’s trigger and bind methods.

    The demo code is more obtuse than it need to be. The network detection object publishes ’status ‘events to the document which actively listens for them and in turn publishes ‘notify’ events to all subscribers (more on those later). The reasoning behind this is that in a real world application there would probably be some more logic controlling when and how the ‘notify’ events are published.

    $(document).bind('status.networkDetection', function(e, status){     // subscribers can be namespaced with multiple classes     subscribers = $('.subscriber.networkDetection');     // publish notify.networkDetection even to subscribers     subscribers.trigger('notify.networkDetection', [status])     /*     other logic based on network connectivity could go here     use google gears offline storage etc     maybe trigger some other events     */ }); 

    Because of jQuery’s DOM centric approach events are published to (triggered on) DOM elements. This can be the window or document object for general events or you can generate a jQuery object using a selector. The approach i’ve taken with the demo is to create an almost namespaced approach to defining subscribers.

    DOM elements which are to be subscribers are classed simply with “subscriber” and “networkDetection”. We can then publish events only to these elements (of which there is only one in the demo) by triggering a notify event on $(“.subscriber.networkDetection”)

    The #notifier div which is part of the .subscriber.networkDetection group of subscribers then has an anonymous function bound to it, effectively acting as a listener.

    $('#notifier').bind('notify.networkDetection',function(e, online){     // the following simply demonstrates     notifier = $(this);     if(online){         if (!notifier.hasClass('online')){             $(this)                 .addClass('online')                 .removeClass('offline')                 .text('ONLINE');         }     }else{         if (!notifier.hasClass('offline')){             $(this)                 .addClass('offline')                 .removeClass('online')                 .text('OFFLINE');         }     }; }); 

    So, there you go. It’s all pretty verbose and my example isn’t at all exciting. It also doesn’t showcase anything interesting you could do with these methods, but if anyone’s at all interested to dig through the source feel free. All the code is inline in the head of the demo page

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

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I like Jon Skeet's answer; it's seeing the forest instead… May 11, 2026 at 1:03 pm
  • added an answer Wherever possible, make your types immutable to start with. Then… May 11, 2026 at 1:03 pm
  • added an answer Well, actually I found a tutorial on MSDN: Walkthrough: Using… May 11, 2026 at 1:03 pm

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I have a web-service that I will be deploying to dev, staging and production.
I'm thinking of starting a wiki, probably on a low cost LAMP hosting account.
I have the following tables in my database that have a many-to-many relationship, which
I'm using the RESTful authentication Rails plugin for an app I'm developing. I'm having
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.