Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
How to set select2 selected value using option text?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!