Tuesday, 6 August 2013

How do i change default option in select Jquery

How do i change default option in select Jquery

I have this select, where i load a bunch of countries. After the user
selects one, he will go to another page where he searches for information.
If he selected a country before he should have that country selected by
default (on another select, in another page, which contains the same
countries).
<div data-role="fieldcontain">
<label for="selecionarPaisPreferencia" class="select">Pais</label>
<select name="selecionarPaisPreferencia"
id="SlSelecionarPaisPreferencia"></select>
</div>
How do i change the default option in a select, before loading the page.
BTW: i have already stored the name of the country he selected in a global
variable.
i load the options dinamically since im not using database this is for an
exercise.
for (i = 0; i < countrys.length; i++) {
$("#SLSelectPaisConsulta").append('<option data-pais="' + countrys[i]
+ '">' + countrys[i] + '</option>');
$("#SlSelecionarPaisPreferencia").append('<option data-pais="' +
countrys[i] + '">' + countrys[i] + '</option>');
}

No comments:

Post a Comment