0

I'm using select2 with a delivery date calendar, available dates are listed like this.

<select name="e_deliverydate" id="e_deliverydate" class="select select2-hidden-accessible" data-placeholder="Escolha uma data" tabindex="-1" aria-hidden="true">
   <option value="select">Select a delivery date</option>
   <option value="18-9-2021">sábado, 18 setembro, 2021</option>
   <option value="21-9-2021">terça-feira, 21 setembro, 2021</option>
   <option value="22-9-2021">quarta-feira, 22 setembro, 2021</option>
   <option value="23-9-2021">quinta-feira, 23 setembro, 2021</option>
   <option value="24-9-2021">sexta-feira, 24 setembro, 2021</option>
</select>

Is it possible to set the selected option using the option text? I'm trying this:

let date_string = 'quarta-feira, 22 Setembro, 2021';

let val = $('#e_deliverydate').find("option:contains('"+date_string+"')").val();

$('#e_deliverydate').val(val).trigger('change.select2');

But all I get to do is to empty the select2 input, I'm using select2 4.0x, what should I do?

1 Answers1

0

Your Date string

let date_string = 'quarta-feira, 22 Setembro, 2021';

Can you try it for val

let val = $("#e_deliverydate option").filter(function() { 

    return $(this).text().includes(date_string) 

}).val();

And this for trigger

$('#e_deliverydate').val(val).trigger('change');
Yahya Altintop
  • 204
  • 2
  • 12