Error while selecting select text

Asked

Viewed 86 times

0

Good evening, folks. I have a select that lists the cities of the respective state. I’m using this code to select a city from the list:

$('#txtCidades option').removeAttr('selected').filter("[text=São Lourenço da Mata]").attr('selected', "selected").change();

When the text is done by more than one word, I get the following error message:

Uncaught Syntax error, unrecognized Expression: [text=São Lourenço da Mata]

But when the city is composed of only one word, it selects normally without error.

Someone gives a light?

  • You can join HTML to complete the question?

3 answers

0


Thus:

$('#txtCidades option').removeAttr('selected').filter("[text='São Lourenço da Mata']").attr('selected', "selected").change();
  • Thank you, uristona. It worked perfectly. The set of double quotes and simple.

0

When you use [text=São Lourenço da Mata] this implies that you have an element in HTML/option with that attribute. That’s how jQuery reads your query. Something like

<option text="São Lourenço da Mata">foo</option>

I imagine that this is not the case but rather you want to filter the textContent option. In this case I suggest you use a function in the jQuery method.

$('ul li').removeAttr('selected').filter(function () {
    return $(this).text() == 'São Lourenço da Mata'; 
 }).attr('selected', "selected");
  • The code is this one: $('#txtCidades option').removeAttr('selected').filter("[text='" + Cidade + "']").attr('selected', "selected").change(); , already with uristone-oriented auteration. City, in this context, is a var that receives a string from a zip code search that returns the city. Therefore, I select the item in the select that has the same City text. Thanks for the reply! And as you and the uristona showed, it was a mistake in the sylthesis of my code. Thanks!

  • @Abelandré I wanted to see the HTML of this select. It is important to put this in the question...

0

Hi, @Sergio, HTML

<select id="txtUF">
    <option value="">Selecione o estado</option>
    <option value="1">AC</option>
    <option value="2">AL</option>
...
    <option value="24">SC</option>
    <option value="26">SP</option>
    <option value="25">SE</option>
    <option value="27">TO</option>
</select>
<br/>
<select id="txtCidades">
    <option>Selecione um estado para listar as cidades</option>
</select>

I have a Webmethod (c#) that carries city listings according to the selected UF. So far so good. But I also implemented the search of the registration data, so I also needed to set the option of select "txtCidades" with the registration city returned.

The problem was this: Webmethod was locating the registration data, but I had neglected the jQuery syntax to assign to the select option the text returned by the query.

the correct jQuery:

`$('#txtCidades option').removeAttr('selected').filter("[text='" + Cidade + "']").attr('selected', "selected").change();`

Browser other questions tagged

You are not signed in. Login or sign up in order to post.