Hello stackoverflow: I am trying to make the $('.TextBoxColors-') part of my code dynamic.
I am looking for the i variable in the for-loop to assign this value depending on the if.
I have tried many things like $('.TextBoxColors-${i}'), $('.TextBoxColors-').value(i) as in $('.TextBoxColors-').value(i).css('background-color', 'green'),
but nothing works so far.
This is the javascript/jquery:
let timeArray = ["9","10", "11", "12", "13", "14", "15", "16", "17"]
let currentTime = parseInt(moment().format("H"));
for (i=0; i< 9; i++) {
if (currentTime < parseInt(timeArray[i])) {
$('.TextBoxColors-9').css('background-color', 'gray')
$('.TextBoxColors-10').css('background-color', 'gray')
$('.TextBoxColors-11').css('background-color', 'gray')
$('.TextBoxColors-12').css('background-color', 'gray')
}
if (currentTime == timeArray[i]) {
$('.TextBoxColors-13').css('background-color', 'Tomato')
}
if (currentTime > timeArray.indexOf("currentTime",currentTime)) {
$('.TextBoxColors-14').css('background-color', 'green')
$('.TextBoxColors-15').css('background-color', 'green')
$('.TextBoxColors-16').css('background-color', 'green')
$('.TextBoxColors-17').css('background-color', 'green')
}
}
These are two of the divs where I want this to happen:
<!-- 2:00PM-->
<div class="input-group input-group-lg field-size Timer" >
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-lg">2 PM</span>
</div>
<textarea type="text" class="TextBoxColors-14 form-control" aria-label="2:00 PM"
aria-label describedby="inputGroup-sizing-sm"></textarea>
<button class="btn btn-primary" type="button" id="saveToDo-5"><i class="fa fa-floppy-o" style="font-size:18px;"></i>
</button>
</div>
<!-- 3:00PM-->
<div class="input-group input-group-lg field-size" >
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-lg">3 PM</span>
</div>
<textarea type="text" class="TextBoxColors-15 form-control" aria-label="3:00 AM"
aria-describedby="inputGroup-sizing-sm"></textarea>
<button class="btn btn-primary" type="button" id="saveToDo-0">
<i class="fa fa-floppy-o" style="font-size:18px;"></i>
</button>
</div>
$('.TextBoxColors').eq(9)instead, this will select all elements of the class, then pick the 10th element.$('.TextBoxColors').eq(9)is something I am still to get it to make sense to me. Thanks!eq()to narrow it down to a single element)