Rav
Rav

Reputation: 1387

How to set default value of a textbox from a jQuery function with a value from MySQL array?

I have this problem where I have to set multple jQuery timepicker's default value (they are put in an HTML table) from the PHP array which is looped.

Here's the JS code for one timepicker textbox:

$(document).ready(function(){

    $('.timepicker').timepicker({
        timeFormat: 'HH:mm',
        interval: 1,
        minTime: '7',
        maxTime: '7:00pm',
        defaultTime: '<?php echo $records[$i]->time_in ?>',
        dynamic: false,
        dropdown: true,
        scrollbar: true
    });
});

</script>

and the table is populated thru a loop:

<table border=1>
   <tr>
     <th>Time In</th>
   <tr>
   @for($i=0;$i<count($records);$i++)
   <tr>
     <td><input type="text" class="timepicker" id=start_shift_$i /></td>
   </tr>
   @endfor
</table>

What I'm trying to do is to populate the timepickers default value with the time_in values looped from an array.

Hope you can help. Thanks!

Upvotes: 1

Views: 149

Answers (1)

Rakesh
Rakesh

Reputation: 505

You can take an extra attribute to the input field as 'default_date' and set the default date value to this attribute.

 <table border=1>
   <tr>
     <th>Time In</th>
   <tr>
   @for($i=0;$i<count($records);$i++)
   <tr>
     <td><input default-date="<?php echo $records[$i]->time_in ?>" type="text" class="timepicker" id=start_shift_$i /></td>
   </tr>
   @endfor
</table>

Now you can access default date attribute like this ...

$('.timepicker').each(function(i, obj) {
     $(this).timepicker({
        timeFormat: 'HH:mm',
        interval: 1,
        minTime: '7',
        maxTime: '7:00pm',
        defaultTime: $(this).attr('default-date'),
        dynamic: false,
        dropdown: true,
        scrollbar: true
    });
});

Upvotes: 1

Related Questions