AftabHafeez
AftabHafeez

Reputation: 173

How to get the dynamic id in javascript

<?php  
  while($array = mysqli_fetch_array($query)) {
      <strong>Recieved Date:</strong>
      <input style="width:70px;" id="rec_date" class="rec_dat" 
             type="text" name="payment_recieved_date">;
  }
?>

And my javascript function is:

 $(function() {
    $(".rec_date").datepicker();
    $('.rec_date').datepicker("option", "dateFormat", 
       "yy-m-d" + " " + getCurrentTime());

        //datepicker({dateFormat: '<?php echo "yy-m-d"; ?>', 
        //   changeMonth: true, changeYear: true, yearRange: '-100:+0'});
    });

I have a problem that calendar is showing and add value for first iteration and the second just showing calendar but no value is added when I select the date.

Please guide me to make id unique at javascript function.

Upvotes: 3

Views: 124

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

You're outputting multiple elements with the same id value. That's invalid, and (I just checked) it's confusing the jQuery UI datepicker.

If you remove the ids, and output the class as rec_date (not rec_dat) on the elements, it works correctly:

$(".rec_date").datepicker();
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<input type="text" class="rec_date">
<input type="text" class="rec_date">
<input type="text" class="rec_date">
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

Upvotes: 2

Related Questions