1

Hi I really need little help with my dynamic table "Timesheet ". I want add datepicker to date text filed dynamically? could anyone hlep me? this code will add new row to table when user click add new row ?

  $(document).ready(function ($) {
    // trigger event when button is clicked
    $("button").click(function () {
        // add new row to table using addTableRow function
        addTableRow($("table"));

        // prevent button redirecting to new page
        return false;
    });

    // function to add a new row to a table by cloning the last row and
    // incrementing the name and id values by 1 to make them unique
    function addTableRow(table) {
        // clone the last row in the table
        var $tr = $(table).find("tbody tr:last").clone();

        // get the name attribute for the input and select fields
        $tr.find("input,select").attr("name", function () {
            // break the field name and it's number into two parts
            var parts = this.id.match(/(\D+)(\d+)$/);

            // create a unique name for the new field by incrementing
            // the number for the previous field by 1
            return parts[1] + ++parts[2];
            // repeat for id attributes
        }).attr("id", function () {
            var parts = this.id.match(/(\D+)(\d+)$/);
            return parts[1] + ++parts[2];
        });

        // append the new row to the table
        $(table).find("tbody tr:last").after($tr);
    };

});

  <table>
    <thead>
      <tr>
        <th scope="col">Date</th>
        <th scope="col">Start Time</th>
        <th scope="col">End Time</th>
        <th scope="col">Hour Type</th>

      </tr>
    </thead>


    <tbody>
      <tr>
        <td><input name="date1" id="date1"></td>
        <td><input name="startTime1" id="startTime1"></td>
         <td><input name="endTime1" id="EndTime1"></td>
        <td>
          <select name="hourType1" id="hourType1">
            <option value="">Please select</option>
            <option value="1">Regular</option>
            <option value="2">Overtime</option>
          </select>
        </td>
      </tr>
    </tbody>   
</table>   
<button>Add Row</button>

2 Answers2

0
addTableRow($("table"));

After you've done that.

$("table tr").last().find('input.date-field').datepicker({...});
//{} = datepicker settings.
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
0

Instead of using id use a class :

HTML

<td><input name="date1" class="date"></td>

jQuery

$(".date").datepicker();
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
fatiDev
  • 5,752
  • 7
  • 28
  • 45
  • hi can you follow updated this post http://stackoverflow.com/questions/12222137/adding-jquery-ui-datepicker-dynamically-style-break –  Aug 31 '12 at 20:39