JSFiddle - React, Tailwind, and code Playground
HTML
<select>
<option value='squeak'>oink</option>
<option value='oink'>gurgle</option>
<option>gurgle gurgle gurgle</option>
</select>
<br>
<br>
<input>
<br>
<button id='method-1'>Select</button>Select option with typed text using custom method from <a href='http://stackoverflow.com/a/16887276/170958'>http://stackoverflow.com/a/16887276/170958</a> - this way works!
<br>
<button id='method-2'>Select</button>Select option using <a href='http://stackoverflow.com/a/1009750/1709587'>.val</a> - note that this doesn't work correctly for the options with value attributes.
<br>
<button id='method-3'>Select</button>Select option using <a href='http://stackoverflow.com/a/13644610/1709587'>:contains</a> - note that this doesn't work correctly if you try to select 'gurgle' here, because the option text is also contained within the 'gurgle gurgle gurgle' option.
JavaScript
jQuery.fn.selectOptionWithText = function selectOptionWithText(targetText) {
return this.each(function () {
var $selectElement, $options, $targetOption;
$selectElement = jQuery(this);
$options = $selectElement.find('option');
$targetOption = $options.filter(
function () {return jQuery(this).text() == targetText}
);
// We use `.prop` if it's available (which it should be for any jQuery
// versions above and including 1.6), and fall back on `.attr` (which
// was used for changing DOM properties in pre-1.6) otherwise.
if ($targetOption.prop) {
$targetOption.prop('selected', true);
}
else {
$targetOption.attr('selected', 'true');
}
});
}
$('#method-1').click(function () {
var textToSelect = $('input').val();
$('select').selectOptionWithText(textToSelect);
});
$('#method-2').click(function () {
var textToSelect = $('input').val();
$('select').val(textToSelect);
});
$('#method-3').click(function () {
var textToSelect = $('input').val();
$('select option:contains("' + textToSelect + '")').prop('selected', true);
});