Question

I have a table and I am adding rows to it dynamically, every one of the rows will have 2 cells, one of them will contain an input field where I plan to use the DatePicker UI to, obviously, pick a date. The issue comes when I add the new row, cells and everything add just fine but the input field somehow does not show the DatePicker when I click on it. Reading around I learned I'm supposed to use "Event Delegation" since the new elements were not loaded from the start. But for the life of me I have not been able to understand how to do it, if someone could point me in the right direction that would be great. Thanks

The code I've got so far:

<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-    ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function() {
  $( ".datepicker" ).datepicker();
  $( "#TheTable" ).on("change",".datepicker",function(){
    $( this ).datepicker( "option", "dateFormat", "yy-mm-dd" );
  });
});

function addRow() {   
    var table = document.getElementById("TheTable");
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML='Date';
    cell2.innerHTML='<input type="text" class="datepicker"/>';
}
</script>
</head>
<body>
<table name="MyTable" id="TheTable">
  <tr>
    <td>Date</td>
    <td><input type="text" class="datepicker" /></td>
  </tr>
  <tr>
  <td>Date</td>
    <td><input type="text" class="datepicker" /></td>
  </tr>
</table>
<input type="button" onClick="return addRow()" value ="Add Another Date">   
</body>
</html>
Was it helpful?

Solution

$( "#TheTable" ).on("change",".datepicker",function() {

This should work.

.on() works by listening to a parent element for events that bubble up (see here for explanation of bubbling - first paragraph), so that events fired by new elements bubble up to the parent that is being listened to.

As a side note, I'd like to mention that it is a good idea to listen to the the lowest common parent of the desired elements, otherwise jQuery will evaluate unnecessary events.

See jQuery docs for .on()

Here are the key points of the .on() docs:

.on( events [, selector ] [, data ], handler(eventObject) )

The majority of browser events bubble, or propagate, from the deepest, innermost element (the event target) in the document where they occur all the way up to the body and the document element. In Internet Explorer 8 and lower, a few events such as change and submit do not natively bubble but jQuery patches these to bubble and create consistent cross-browser behavior.

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()

OTHER TIPS

You need to be listening to the parent to be able to target dynamically drawn content. Replace:

$(document).ready(function() {
  $( ".datepicker" ).datepicker();
  $( ".datepicker" ).on("change",function() {
    $( ".datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
  });
});

with:

$(document).ready(function() {
  $( ".datepicker" ).datepicker();
  $( '#TheTable' ).on("change",'.datepicker',function() {
    $( this ).datepicker( "option", "dateFormat", "yy-mm-dd" );
  });
});

You don't need event delegation; just set your options at the beginning. Then, in your function, trigger the datepicker method (with the desired options) on the new element as you're adding it.

$(".datepicker").datepicker("option", "dateFormat", "yy-mm-dd");

function addRow() {   
    var table = document.getElementById("TheTable");
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML='Date';
    $('<input type="text" class="datepicker"/>')
        .datepicker("option", "dateFormat", "yy-mm-dd")
        .appendTo(cell2);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top