
|
Auto-select an Element from a Menu or Scrolling List & Save Keystrokes! By Nannette Thacker - 08/23/2000 The above form may not look like much, but with my "fmtSelectItem()" function, I created my <select> form element scrolling list quickly with very little typing -- AND automatically defaulted to the appropriate selection in the list. For instance, in your customer table, you know your customer's favorite fruit is "Bananas" by looking it up in the table. Pass that value into the function, and it will automatically compare the customer's favorite fruit to the value in the option tag. If it matches, it will add "selected " to the option tag -- saving you from a lot of keystrokes! Here is the ASP code to generate the form:
In this page's example, I use the request.form to populate the default value. If it is empty, I set the default to "Bananas." However, in reality, you would probably use a value from a table record, and the form would likely not just continually call itself. :) But notice how simple it was for me to create the option tag for each element in the scrolling list. All I had to do was type (or cut and paste) this several times, replacing "Apples" with the appropriate fruit:
call fmtSelectItem("Apples",mylist)
Your code as presented to the browser now looks like this:
<form action="select.asp" method="POST" id=form1 name=form1> <select NAME="mylist" size="4"> <option value="Apples">Apples</option> <option value="Oranges">Oranges</option> <option value="Grapes">Grapes</option> <option value="Pears">Pears</option> <option value="Cherries">Cherries</option> <option value="Tangerines">Tangerines</option> <option selected value="Bananas">Bananas</option> </select> <input type="Submit" name="Submit" value="Select"> </form>And here's the function that does all the work:
In the above function, you've passed in the "sItem" or the Fruit Name to be displayed in the list and to be used as the "value" of the option. You've also passed in "myItem" which is the value you are comparing against the value of the current element in the list. If the value of "myItem" is equal to the value of "sItem" then you add "selected" to the option tag. This function can also be used when creating your list of options by traversing a table record set. For instance your list may be populated from a table of zip codes. This can also be used for a list of States or Provinces and you wish to pre-select the user's State. However, I would rewrite the function to pass in 3 values.
|
|
