2

At the outset, let me be clear I'm not trying to use load() in an Ajax context to load a remote resource.

I'm just trying to bind a function to an object that doesn't exist at page load time, such that I can do stuff to it when it does appear. I'm using jQuery 1.7

I have a form with class="contact-form"). This form is created on the fly, so it doesn't exist when document.ready() fires.

What I want to do is make some stuff happen when the form is created. Presumably there should be a "load" or "ready" or some such event fired when the thing is available. Under previous versions of jQuery I'd have used delegate() or live(); but these have been deprecated, and the current documentation says to use on( "load", handler ) or its shortcut, load(). I'm getting this from https://api.jquery.com/load-event/.

All of the following have so far failed to work:

 $(".contact-form").load(function(){
    console.log("Hi there!");
  }); 

and

 $(".contact-form").on("load", function(){
    console.log("Hi there!");
  }); 

and, in a hail-mary based on ideas from Jquery event handler not working on dynamic content,

 $(document.body).on("load", ".contact-form", function(){
    console.log("Hi there!");
  });      

Any pointers appreciated.

Community
  • 1
  • 1
Sam Moore
  • 77
  • 3
  • 6
  • There's no such thing, some elements, such as images, iFrames etc. have a load event, but you can't bind that event on something that doesn't exists, and there's no such event for other elements, closest you'll get is mutation observers. – adeneo Mar 11 '14 at 21:21
  • From JQuery docs (Same link): This event can be sent to any **element associated with a URL**: images, scripts, frames, iframes, and the window object. – palanik Mar 11 '14 at 21:22
  • Above comments are correct. How is `.contact-form` inserted into the page? – Will Mar 11 '14 at 21:24
  • The form is assembled via some javascript that I don't have control over, in response to a user's clicking a button. – Sam Moore Mar 11 '14 at 21:30

3 Answers3

3

If you use .load() which is a shortcut for .on('load') called the load event, the matching element (form in this case) must exist at the time the page was loaded. jQuery < 1.7 had a .live() function which would listen for new elements dynamically added to the page, but it was removed in jQuery 1.7 for various reasons, performance being a major one.

Other options

jQuery LiveQuery is a plugin that sounds like it will do exactly what you're looking for. https://github.com/brandonaaron/livequery

jQuery Entwine will watch for new DOM elements using livequery, but also allows you to create DOM elements and use them as full objects with their own methods defined. https://github.com/hafriedlander/jquery.entwine

More info from jQuery's .on() docs

You can use Delegated events to create a click handler which will fire when an element is dynamically added to your original selector (typically a container), such as:

$( "#dataTable tbody" ).on( "click", "tr", function() {
  alert( $( this ).text() );
});

Now, when a new <tr> is added dynamically, it will have the click handler bound to it. However, there is no event for the actual loading of an element into the DOM.

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

0

Why do you need an event at all? If the form is being added dynamically then run what you need to at the time

var form = '<form class="contact-form"></form>';
$('body').append(form);
console.log("Hi there!");
andrew
  • 9,313
  • 7
  • 30
  • 61
0

This method is a shortcut for .on( "load", handler ).

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

For example, consider a page with a simple image:

<img src="book.png" alt="Book" id="book">

The event handler can be bound to the image:

$( "#book" ).load(function() {
  // Handler for .load() called.
});

As soon as the image has been loaded, the handler is called.

Now put that inside a ready handler

$( document ).ready(function() {
  // onload functions here
});
MGot90
  • 2,422
  • 4
  • 15
  • 31
  • 1
    Yes, saw that, thanks, but perhaps the load event doesn't fire for every type of object...? – Sam Moore Mar 11 '14 at 21:33
  • Adding your script to the end of the body will guarantee the form has loaded. Please thumbs up if this helped you! – MGot90 Mar 11 '14 at 21:35