10
I have a select and would like to select a value according to the text of another field that the user clicked.
For example. By clicking a "p" tag with the "April" text 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=] doesn’t work so what would be the most elegant way to do it, without having to loop in select for example?
João, I know your question was about how to search the text, but if we are going to evaluate the performance it would not be better to assign for example the id in the tag
<p>
and fetch the value where the id is equal?– Alessandro Gomes
Yes, but it is an urgent implementation and due to the conditions that exist in the generation of the code by the server I would have to change in several places and in js only in one.
– Joao Paulo