1

As the title says, I'm trying to update dropdowns I've populated on a timesheet with jQuery every time I load my table. So there are like 28-31 days and each day has its own dropdown with id="dayType1" all the way up to 31. I was hoping there is a way to iterate on the jQuery selector but from what I read it is not possible? I tried to put each time it runs over the iteration a new ID together like a string and then paste it into the function as a selector, but that doesn't seem to work either. So now I call upon the jQuery gods here to help this peasant. :)

I keep the values for the dropdowns in a separate list so as the iteration goes the list has the values that I need to change the dropdowns to in order at the same spot. (So dropdown 8 needs to be set as the integer saved in the list on the 8th spot.)

First I had timesheets of only a week and could do this:

function setDayTypes() {
    $('#dayType1').val(dayTypeList[0]);
    $('#dayType2').val(dayTypeList[1]);
    $('#dayType3').val(dayTypeList[2]);
    $('#dayType4').val(dayTypeList[3]);
    $('#dayType5').val(dayTypeList[4]);
    $('#dayType6').val(dayTypeList[5]);
    $('#dayType7').val(dayTypeList[6]);
}

But now I have timesheets of a month and not all months are the same. Is there a clean way to iterate over these id's? In the dayTypeList are the values for the inputs.

enter image description here

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Show us the code. Id's have to be unique. – Grumpy Apr 27 '22 at 15:16
  • I know, and they are. It's #dataType1 up to #dataType31 based on the month lenght. The previous picture didn't really show that. This one should give you a better idea. – Guy De Roover Apr 27 '22 at 16:58
  • Please show us your selectors. You could use jQuery `.each()`: https://api.jquery.com/each/ or the `.forEach()` of a *NodeList* (from `.querySelectorAll()`): https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach – BairDev Apr 27 '22 at 17:12
  • You haven't shown us any code, so it is hard to offer any help. If iterating over your selector is the problem, a search for "*jquery iterate selectors*" turns up plenty of examples here on SO, eg https://stackoverflow.com/questions/4735342/jquery-to-loop-through-elements-with-the-same-class Edit your question, show us 'the minimal code](https://stackoverflow.com/help/mcve) to reproduce the problem you're having. – Don't Panic Apr 27 '22 at 17:35
  • Thanks, I didn't think the "jquery iterate selectors" would work for that. Works like a charm. I only used them for iterating through ajax/JSON data before. – Guy De Roover Apr 27 '22 at 18:59

1 Answers1

1

This works great, thanks.

$( "td .dayType" ).each(function( index ) {
   $(this).val(dayTypeList[index])
});