0
I am inexperienced in javascript, and would like a help in this case:
I have two selects in my document:
<select id="estado">
<option value="MG">MG</option>
<option value="SP">SP</option>
<option value="RJ">RJ</option>
<option value="RJ">BA</option>
</select>
<select id="cidade">
<option value="Belo Horizonte">Belo Horizonte</option>
<option value="Betim">Betim</option>
<option value="Contagem">Contagem</option>
</select>
The expected behavior is that by clicking on an option of the state, it replaces the select of id=city. I believe that Document.write is the solution, however, the content to be replaced should be taken into account the order of the option clicked in select id=status and not its value.
For example:
The first option replaces the select city for:
<select id="cidade">
<option value="Sou o primeiro">Sou o primeiro</option>
</select>
The second option replaces the select city for:
<select id="cidade">
<option value="Sou o segundo">Sou o segundo</option>
</select>
The third replaces the select city to:
<select id="cidade">
<option value="Sou o terceiro">Sou o terceiro</option>
</select>
The room replaces the select city for:
<select id="cidade">
<option value="Sou o quarto">Sou o quarto</option>
</select>
The bigger problem is that these values to be replaced should be referenced by the order in the select state, not its value. That is, no matter the value of the third option of the select #state, no matter if it will be SP, RJ, MG or BA, when it is clicked, it will replace the other select with the third code above. If you click on the second option, replace it with the second code.
Well that’s it. I thank everyone who helps.
You want to replace the
select#cidade
in whole or only its "Content", the<option>
? I’m not sure I understand your question– Richard Dias
Are you using any lib, like
jQuery
for example?– Richard Dias
You want when selecting a state, in the select of cities to be loaded a list of the respective ones to that state?
– Luis Henrique
@I’m not using libraries. The idea is that by clicking on the first option of the #state, replace the #city list with the List referring to the first option. That is, no matter the values, the substitution is fixed by the order of the first, second, weaver option. It can only be the content of the options, or the easiest.
– Boni
Boni, in this type of situation, I advise you to load the second select with AJAX. If you have any questions about how to do it, just tell me what technology you’re using in Server-Side, PHP, Ruby, C#, etc. If it’s just an academic activity, you can keep lists of cities, and just reload content.
– Tobias Mesquita
@Tobymosque It is a very simple activity, do not need AJAX no. It is not necessary to take into account the cities. As I said, the codes of the first, second, weaver option are fixed, the code of the first option must appear independent of the value you have. And so goes the second, third, etc.
– Boni
The question is... what is the need for it? Explain to us, who knows we can not understand better and help you
– MarceloBoni