2

I am using jQuery to attach a function to my click event for an entire class. For example:

$(".clickDiv").click(function(){
   $(this).hide();
});

On my client-side javascript, I create more .clickDiv instances dynamically.
Do I need to call the $(".clickDiv).click(function...) again, or will the new instances automatically have that function bound to the click event?

gdoron
  • 147,333
  • 58
  • 291
  • 367
steve8918
  • 1,820
  • 6
  • 27
  • 38
  • 1
    No I did something similar but new instances were not automatically bound, I had to bind them using .live() function. You can check http://api.jquery.com/live/ on jQuery website. –  Apr 09 '12 at 03:06
  • 1
    @anuragsn7. Enough with `live`! read my updated answer. Too much people are using legacy code... `on`,`on`,`on`,`on`,`on`,`on`,`on`,`on`,`on`,`on`,`on`,`on`,`on`,`on` **!!!** – gdoron Apr 09 '12 at 03:27

3 Answers3

7

Yes you do, unless you use a delegate event

like this:

$('#container').on('click', '.clickDiv',  function() {
   $(this).hide();
});

on docs:

If selector is omitted or is null, the event handler is referred to as direct or directly-bound. The handler is called every time an event occurs on the selected elements, whether it occurs directly on the element or bubbles from a descendant (inner) element.

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector.

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.


Just because too much people here suggested you should use live, live is deprecated since version 1.7 by on and was replaced in version 1.4.3 by delegate

$(selector).live(events, data, handler);                // jQuery 1.3+
$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+
Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Thanks, this was a great answer! I'll read up more on delegate events. – steve8918 Apr 09 '12 at 03:19
  • 2
    +1 Great answer, thanks for bringing that to my attention (that `.live` is deprecated - I've been using it all along). But I won't be changing it anytime soon (as I said in my comment on the other answer) – Nathan Apr 09 '12 at 03:24
  • I use very limited functions from jQuery so I have been using older version of jQuery for quite some time. Thanks for this useful information. –  Apr 09 '12 at 03:41
1
I am attaching an event like : 

$('body').on('click', 'button[data-tracking], a[data-tracking]',

                function(event) { console.log($(event.target));
});

and want to get to the target of element which is set up as :
  <pre> <[]a data-tracking="hello" href="hello">
    \<\span\>test now\<\/span\>
   \<\/a\>
 </pre>
it does work perfectly, but event.target gives me "span" element but what i want is "a" element so that I could get to value of data-tracking attribute. 
mmm
  • 11
  • 2
0

This will bind the event to all new instances

$('.clickDiv').live('click',  function() {
   $(this).hide();
});

http://api.jquery.com/live/

Shyju
  • 214,206
  • 104
  • 411
  • 497
mike clagg
  • 830
  • 7
  • 12
  • 4
    NOOOO!!! not `live` again! it's deprecated and should NOT be used since jQuery 1.4.4! – gdoron Apr 09 '12 at 03:10
  • 1
    @gdoron Actually: `As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.` Where did you get "since 1.4.4" from? – Nathan Apr 09 '12 at 03:18
  • @gdoron Oh, well I'll continue to use `.live` because I am using it all over my files and there's no way I'm changing it now, unless it doesn't work anymore then I'll change it. _Not cool jQuery team, not cool._ – Nathan Apr 09 '12 at 03:23
  • @gdoron Well, it's still the jQuery team's fault for removing a useful feature and replacing it (just to save 2 characters of text - really?) Why did they deprecate it? It worked fine. Are there any benefits to using `.on` vs `.live`? – Nathan Apr 09 '12 at 03:27
  • @Nathan. You missed the whole point of `on` V.S. `live`. read [this](http://stackoverflow.com/a/9419050/601179) answer. and the [live docs](http://api.jquery.com/live/). specially this: `Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled` – gdoron Apr 09 '12 at 03:30
  • @gdoron Oh, ok. I don't notice any speed differences though :P – Nathan Apr 09 '12 at 03:34
  • @Nathan. Well... it's your code, but I would have stop using `live` as it slower and has no advantage over `on`. – gdoron Apr 09 '12 at 03:37
  • 1
    @gdoron Okay next time I need to bind something to dynamic elements I will use `on` :) – Nathan Apr 09 '12 at 03:39