I have a select and would like to select a value according to the text of another field that the user clicked on.
For example. When I click on a "p" tag with the text "April" I want to select the "April" text option in my select:
<select id="meses">
<option value="0">Janeiro</option>
<option value="1">Fevereiro</option>
<option value="2">Março</option>
<option value="3">Abril</option>
</select>
My tag p:
<p class="nomMes">Abril</p>
I wanted something like this:
var valMes = $('p.nomMes').text().trim();
$(document).on('click', 'p.nomMes', function(){
$('#meses option[text='+valMes+']).prop('selected', true);
});
But the option [text =] does not work so what would be the most elegant way to do this, without having to loop through the select for example?