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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:57:15+00:00 2026-06-16T17:57:15+00:00

I have a jQuery plugin that loads asynchronously and attaches events to a DOM

  • 0

I have a jQuery plugin that loads asynchronously and attaches events to a DOM element on page load.

Intermittently the DOM elements are not attached with events from the plugin – This happens due to the loading of these DOM elements via another javascript file.

Is there any way to ensure that event handlers are always attached to DOM elements?

  • 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-16T17:57:16+00:00Added an answer on June 16, 2026 at 5:57 pm

    The main answer here is that if you’re writing a plug-in, it’s not your problem to attach it to the right elements on page load (or at any other time). That’s for the user of your plug-in to do. It’s not in your problem domain.

    But if you want to try to handle it:

    You can ensure that the page is fully loaded by using the jQuery ready handler, which has as shortcut (just pass a function into jQuery), e.g.:

    jQuery(function($) {
        // ...here, all page elements defined by the main HTML have been created
    });
    

    If you need to handle elements that are added after the page is fully loaded, look at using event delegation. That’s where you handle events not on the elements themselves, but on their ancestor elements, when the events bubble up to them. For instance, this will handle any click event on an element with class foo unless the event is cancelled by a handler attached closer to the element:

    $(document.body).delegate(".foo", "click", function() {
        // An element matching ".foo" was clicked
    });
    

    What delegate does is hook the event on the element you call it on (document.body, in this case), but then only trigger the handler if the actual click had a matching element in the ancestory path from the element where the click actually occurred, to its parent, to its parent, and so on until the element on which you called delegate.

    With recent versions of jQuery, you can also use the hyper-overloaded on function to do the same thing (note the order of the args is different):

    $(document.body).on("click", ".foo", function() {
        // An element matching ".foo" was clicked
    });
    

    I prefer delegate for clarity.

    But again, it’s really not your problem. It’s up to the users of the plug-in to know when, and where, to hook it up.

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

Sidebar

Related Questions

Using the jquery metadata plugin, I have an element on my page that looks
I have a jQuery plugin that moves an element on the screen, but has
I have a simple webpage that uses the jquery template plugin to dynamically load
I have some JQuery that loads one of many elements, for example one <li>
I have a small jQuery plugin that I use for form AJAX validation. There
does jquery have any plugin that prevents entering any input to a textbox that
I have a function called Colorbox (jQuery plugin) that takes a number of parameters
I have been trying to make this to be a little jQuery plugin that
I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want
Does jQuery validation plugin have some dependencies? For example, I saw blogposts that said

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.