I am trying to make a form which let you see boarding times per day. The table is created with PHP. Having a row for every day of the week, except Saturday and Sunday:
<?php
for ($i = 1; $i < 8; $i++){
$d=strtotime("+".$i." Day");
if (date("l", $d) !== "Saturday" && date("l", $d) !== "Sunday" ){
$daydate = $dayOfWeek[date("l",$d)] . date(" d ", $d) . $month[date("F",$d)];
echo "<tr>";
echo "<td>". $daydate . "</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo '<tr class="BoardingTimeMorning">';
echo '<td style="padding-left: 30px">
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input> <div class="time">0:00</div>
</td>
<td>
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input><div class="time">0:00</div>
</td>
<td>
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input><div class="time">0:00</div>
</td>
<td>
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input><div class="time">0:00</div>
</td>
</tr>';
}
}
?>
When someone selects their destination, the corresponding times need to show in the table above. Selection is done with AJAX request and returns the times in an array. No more than 4 boarding times are possible.
I am trying to get it working with JQuery, but nothing happens.
JQuery:
$('#boardingplace').change(function() {
$.ajax({
type: 'POST',
url: 'reservePage.php',
dataType: 'JSON',
data: {
'station': this.value
},
success: function(response){
var len = response.length;
console.log("length: "+len);
$( ".BoardingTimeMorning" ).each(function() {
for (var j=0; j<4; j++){
//visibitly off
var tdContent = $(this).find('.BoardingMorning'+j);
console.log (tdContent);
tdContent.css("display", "none");
tdContent.next().html("");
//reset value
tdContent.attr('value', "");
if(j - len <= 0){
//show content according to amount of times
tdContent.css('display', "block");
tdContent.attr('value', response[j].slice(0,5));
tdContent.next().html(response[j].slice(0,5));
}
}
});
}
});
});
What is the best way to get it to work?
EDIT: Turns out I use 2 class definition on the input element. The bootstrap class and another for selecting it. I put them together and all works.. Thanks for all you help people! Really love this community!
changeevent is not bound correctly, then I doubt that nothing is happening. However there are a number of places it could go wrong, and the code alone is not generally enough to be sure where it is.var tdContent = $(this).find('.BoardingMorning'+j);will never work - the only elements with the class "BoardingMorning" in your code are the radio buttons, but they literally have the class "BoardingMorning"...however the code is asking to find "BoardingMorning0", "BoardingMorning1" etc etc, due to the addition of the value ofjto the selector. You don't have any elements with those classes, so it will never select anything.