0

I want to set re-select option in , I know there is a function to do that for single element using javascript but I use it as an array so I need to set the value inside the loop su as this example:

<select name="days" value = "3">
    <option value="1">Sat</option>
    <option value="2">Sun</option>
    <option value="3">Mon</option>
    <option value="4">Tue</option>
    <option value="5">Wed</option>
    <option value="6">Thu</option>
    <option value="7">Fri</option>
</select>

I think this example will work fine with react, but can I use such a code in normal html and javascript inside the loop.

if you have a database store the day value, instead of use if else function, filter the day on select tag to make the option selected as value in database

basically inside foreach there is a select option tags I need to set selected using the value in database like so:

$('#dynamic_field').append('<select><option value="1">Sat</option><option value="2">Sun</option> ..... </select>

Thanks

aynber
  • 22,380
  • 8
  • 50
  • 63
zippax
  • 284
  • 2
  • 5
  • 13
  • Does this answer your question? [default select option as blank](https://stackoverflow.com/questions/8605516/default-select-option-as-blank) – Christos Lytras Mar 23 '20 at 15:08
  • no totally is not what I need, if you have a database store the day value, instead of use if else function filter the day on select tag to make it selected option as in database – zippax Mar 23 '20 at 15:14

1 Answers1

0

In the end of the loop:

$('select[name^=day]',item.id).each(function(a,b){ $(b).val(item.day); });

with that will check for each value and set the value in the database as selected in select form

*Note: item.id and item.day came from database

zippax
  • 284
  • 2
  • 5
  • 13