Jump to content

How Do I Dynamically Set The Selected Index Of A Dropdown?


Recommended Posts

Hi

 

I am using a combination of javascript and php.

 

I use javascript to populate the country dropdown. For example if you selected united states, there is a state_prov dropdown list which updates to display a list of states, if you select Canada, the state_prov is populated with provinces and you select Australia, the state_prov dropdown list is hidden.

 

This works great, except when the record is being added to a table, but when I have the modify form displaying I can't set the selectedIndex of the state_prov to the stateprov value I retreive from the table.

 

Is there some Javascript code I can use to do this?

 

I have renamed my functions.js to functions.txt so that it can be uploaded here.

modify.php

functions.txt

Here's an example of this I've already done for someone.

http://forums.phpfreaks.com/topic/270342-after-picking-us-or-canada-have-the-appropriate-states-or-provinces-show/

 

In the lower $.each, you could just do an if statement for the state of choice and set the selected attribute when you add the one you want.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.