callback="plusone_vote"

Populating dropdowns

Posted in: - Jun 01, 2011 4 Comments

This example shows how to use the preOpen event in a dropdown. This is the ideal event to use when dynamically setting the items that are available in the dropdown list. The script can be set to look at the values of other objects in the form and then populate the dropdown list based on these values.

I have amended the example, so that if the user deselects a checkbox the script in the click event will check to make sure that the user hasn’t already selected one of the choices in the dropdown. This is done using the string.search() method, which uses a regular expression. Check out the amended script in the click event of the checkboxes.

Download: Assure Dynamics Populating dropdown lists (Rev 1)

4 Responses to “Populating dropdowns”

  1. Reply Jim says:

    The code works great….just what I wanted to do. The only issue with my application is that I would like to remove the dropdown choice if the checkbox is unchecked. The current code will allow “apples” to stay the dropdown choice if fruit is unchecked. Thanks for the help.

  2. Reply MB says:

    Hi, thanx for the info. Been quite helpful in what I need to accomplish as well. One thing I’m battling with is as follows. I use two dropdown lists. list1 one got static data for selection and then list2 get populated based on list1 selection. This works 100% with the preOpen event.

    What I need to do now is, when the value change in list1 the info in list2 gets cleared. Currently there is only two values to choose from, but the list will expand as time goes.

    So I suppose I need to use the change event but how to clear the value?

    Regards,

    • Reply NiallODonovan says:

      MB,

      If you have a look at the click event of the checkboxes, you will see a section of script that deals with this:

      // Check to see if the user has already selected
      // from this option in the dropdown
      var vChoice = userSelection.rawValue.search(/fruit/);
      if (vChoice !== -1) {
      userSelection.rawValue = null;
      }

      This searches the rawValue of the dropdown, looking for the criteria. If the search matches the criteria, it returns the index. If it does not match, it returns -1. This is why I am testing in the if statement for the search !== -1, which would mean that the user has selected a choice that matches the criteria and therefore we much clear the dropdown.

      The easiest approach for you is to include this script in the exit event of the first/static dropdown:

      list2.rawValue = null;

      This will always null the second/dynamic dropdown, every time the user exits the first dropdown.

      You can use the more detailed script block as a basis for a more refined solution, which will take into account your data structure.

      Hope that helps,

      Niall

Leave a Reply