1

I have some javascript that creates a datetime picker on each row of a table. The function only works on the first row. I understand that the reason for this is that the datepicker shares the same ID on every row. How can I adjust my code to fix this?

<script type="text/javascript">
$(document).ready(function (){
$('#duedate').datetimepicker({
controlType: 'select',
timeFormat: 'hh:mm tt'
});
});
</script>

<?php $txtJob = $_GET['pickjob']; ?>

<?php
$query2 = "Select Work_Center, Sequence, Est_Total_Hrs from V_schedule WHERE job  = '" . $txtJob . "'";
$results2 = sqlsrv_query($connPpp, $query2);?>

<form id="frmpromiseddate" name="frmpromiseddate" action="schedule_job_submit.php" method="POST">
       <table  class='table table-bordered table-condensed table-striped'>
           <tr>
               <td>Sequence</td>
               <td>Work Center</td>
               <td>Due Date</td>
           </tr>
<?php while ($row2 = sqlsrv_fetch_array($results2)) {?>
         <tr>
            <td><?php echo $row2['Sequence']?></td>
            <td><?php echo $row2['Work_Center']?></td>
            <td><input type="text" name="duedate" id="duedate" value="" />    </td>
         </tr>
<?php ;} //End of while ?>
</table>
1
  • 4
    Change id (#) to class (.). Commented Mar 11, 2016 at 20:48

2 Answers 2

2

Jquery treats idas unique identifier and apply on only one element at a time, where as it treats class as a group-identifier and can apply to multiple element at a time so change id to class both in html and Jquery code:-

<td><input type="text" name="duedate" class="duedate" value="" /></td>

And

$('.duedate').datetimepicker({
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That was perfect
1

Use class(.) instead of id(#) for date picker

Javascript code :

$('.duedate').datetimepicker()

HTML code: making id unique

<?php $i=0;
while ($row2 = sqlsrv_fetch_array($results2)) {?>
     <tr>
        <td><?php echo $row2['Sequence']?></td>
        <td><?php echo $row2['Work_Center']?></td>
        <td><input type="text" name="duedate" class="duedate" id="duedate_<?php echo $i;?>" value="" />    </td>
     </tr>
<?php $i++; } //End of while ?>

1 Comment

it's not and keeping the ID around can be useful - for example if OP decided he wanted to add labels to his markup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.