I have some elements of the page being injected by a library after the page has been loaded. These elements have onClick actions, which is controlled by jQuery. Since these items are rendered after the page has been loaded, would jQuery have an issue targeting these items to apply the onClick action?
-
2It can, you can "rebind", if I may, the elements click with either .on()/live() depending on what version of jQuery you're using. – Henrik Andersson Nov 13 '12 at 19:41
-
@limelights jQuery 1.7.2. `on()` worked beautifully :) – Jon Nov 13 '12 at 20:04
2 Answers
This is a typical case for using delegated events where you bind the handler to the parent element with a specific selector passed as an argument and jQuery will trigger the handler only when the matching selector is the one triggering the event.
Read more on jQuery documentation under Direct and delegated events -> http://api.jquery.com/on/
.on( events [, selector] [, data], handler(eventObject) )
events
- One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin".
selector
A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.
data
Data to be passed to the handler in event.data when an event is triggered.
handler(eventObject)
A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false.
The above is to target the dynamic elements that could be added later to DOM.
Note:
Incase of delegated events, You should always bind it to closest static container that would be available when you bind the handler. More info: Should all jquery events be bound to $(document)?
However, it is always better to bind directly to the elements which can be achieved by binding the handler after the elements are inserted to DOM.

- 1
- 1

- 79,297
- 15
- 120
- 134
-
good answer with the "Note" - bascially to bind to the closest element to the target so it does not bind to the document. – Mark Schultheiss Nov 13 '12 at 19:54
-
@MarkSchultheiss True and updated the answer with a link discussing the same – Selvakumar Arumugam Nov 13 '12 at 19:58
-
One question... would `on()` be able to target both elements that are loaded initially, and elements that are later injected into the DOM? – Jon Nov 13 '12 at 20:03
-
@icu222much Yes it would, but as mentioned in my 2nd Note.. it is always better to bind it directly to the element when it is available. – Selvakumar Arumugam Nov 13 '12 at 20:04
-
Sorry, for some reason my eyes skipped over that line. I was just wondering so I wouldn't have to write duplicate code twice as I don't know if my client will have some of the elements loaded initially and then inject the remainder later. Thank you for clearing it up for me:) – Jon Nov 13 '12 at 20:07
Use the form:
$("#myid").on("click",".myTargetElement", function(){
// do my click stuff here
});
Sample markup:
<div id="myid">
<div class="myTargetElement">Click Me</div>
<div class="myTargetElement">Click Me</div>
</div>
NOTE: this form binds to the document not the parent element, forcing document traversal and thus not recommended optimally:
$(".myTargetElement").on("click", function(){
// do my click stuff here
});
Example: clones the first element in the markup above and makes them blue when clicked:
$("#myid").on("click",".myTargetElement", function(){
$(this).css('color','blue');
});
$('.myTargetElement').eq(0).clone().text("New").appendTo('#myid');

- 32,614
- 12
- 69
- 100
-
One question... would `on()` be able to target both elements that are loaded initially, and elements that are later injected into the DOM? – Jon Nov 13 '12 at 20:03
-
@icu222much - yes, pretty much the point - it binds to the parent element (or document) and then "finds" the target elements from there - so they would need to be "in" the parent element which would need to exist at the bind time - when it runs initially. – Mark Schultheiss Nov 13 '12 at 20:05
-
I added an example so you could see it in action if you liked. – Mark Schultheiss Nov 13 '12 at 20:16