max7
max7

Reputation: 798

How to select option from dropdown and execute onchange Jquery

I'm probably doing this in the worst way but I'm trying to a) select a dropdown option based on received data from a social networking plugin and b) force an onchange event to make the successive dropmenus appear and populate.

I'm working with country, state and city data. Upon page load, the country dropdown auto populates with two selections: Mexico and United states. When one is selected, the state dropdown auto populates and once a state is selected the city dropdown populates.

here's the markup:

<tr>
<td id="field-country" class="tags-form"></td>
   <td><select onchange="CreateCountries.getRegions(this.value);"
name="country_id" id="country_id" value="" style="width: 130px;"
class="campos-tabla"></select></td>
</tr>
<tr>
   <td id="field-state" class="tags-form"></td>
   <td><select onchange="CreateCountries.getCities(this.value);"
name="state_id" id="state_id" value="" style="width: 130px;"
class="campos-tabla"></select></td>
</tr>
<tr>
   <td id="field-city" class="tags-form"></td>
   <td><select name='city_id' id="city_id" value=""class="campos-tabla"
style="width: 130px;"></select></td>
</tr>

and here's my attempt to a) select dropdown option based on data received and b) activate the onchange event so that the next dropdown menu will populate and then continue with my next selection:

_fillFields: function(){
 logingygia.fields.firstname.val(logingygia.firstname);
 logingygia.fields.lastname.val(logingygia.lastname);
 logingygia.fields.email.val(logingygia.email);
  if (logingygia.gender == 'm') {
    $('#genderM').attr('checked',true);
        }
        else {
            $('#genderF').attr('checked',true);
        };
 logingygia.fields.birthmonth.val(d2(logingygia.birthmonth));
 logingygia.fields.birthday.val(d2(logingygia.birthday));
 logingygia.fields.birthyear.val(logingygia.birthyear)
if (logingygia.country == 'United States') {
        $('#country_id').val(212);
            }
            else {
                $('#country_id').val(143);
            };
            if $('#country_id option: selected ').length){

                if ($('#country_id option:selected').length)
                    $('#country_id').val("state_id).change();
                                }else{
                                  return ("");
            };
            }
};

The if (logingygia.country by itself will select the country correctly. Unfortunately, when I combine that with the "if country_id: option", no country is selected and the state dropdown will show up with only "Selecciona" but does not populate with any choices ... which makes sense since country was not selected.

I'm sure the code is a mess so I would greatly appreciate some better minds to help me.

Upvotes: 0

Views: 1854

Answers (1)

James Montagne
James Montagne

Reputation: 78710

You're missing a parenthese on the one if, and I'm not sure if you can have a space after the :. Also, you're missing a quote. And you have the same if twice.

if ($('#country_id option:selected').length){
   $('#country_id').val('state_id').change();
}else{
   return ("");
};

Fix up the syntax and see if that fixes it.

Upvotes: 1

Related Questions