Pre-Populating SELECT Boxes with HTC Controls


When building web based applications, I frequently have a need to pre-populate the value of SELECT controls.  I found this code some years ago on Microsoft.com but don't remember the original author, but I certainly thank him for making several of my web-based projects considerably more user-friendly.

<html>
<public:component>
<attach event="oncontentready" handler="initialSelection" />
</public:component>

<script type="text/javascript">
// You must specify "initialValue" as a name-value pair on the combo-box
// Example: <select initialValue="TEST"></select>

// Also, the behavior of the SELECT objects must be referenced in the object's style
// Example:
BEHAVIOR: url('/includes/[this file name].htc');

function initialSelection() {
   for (var i = 0; i < this.options.length; i++) {
      if (this.options[i].value == this.initval) { 
         this.options[i].selected = true;
         }
      }
}
</script>
</html>

Comments (0)

Skip to main content