Javascript Combo Boxes at dev.netcetera.org

CONTENTS

Intro

Add combo-box drop-down functionality to existing text boxes, with a drop-down list populated either from a pre-cached array of values or dynamically via Ajax.

Download

dropdown.js.zip

Demo

Type ap into the input box...

Synopsis

<head>
<script type="text/javascript" charset="utf-8">
function create_dropdown() {
    var a = new Array();
    a.push('accident');
    a.push('aphid');
    a.push('apple');
    a.push('application');
    a.push('appelation');

    var s          = document.getElementById('s');
    var d          = new DropDownSelector(s);
    d.delay        = 200;
    d.maxEntries   = 10;
    d.setDataSourceArray(a);
}
</script>
</head>
<body onload="create_dropdown()">
<p>
    <input type="text" id="s" />
</p>
</body>

Documentation

Methods

DropDownSelector(object)

Constructor.

object
The textbox object to which the drop-down is to be attached

setDataSourceArray(array)

Sets the data-source for the drop-down to an array of strings.

array
The of strings to be used as the data source for the drop-down

Properties

delay

The delay in milliseconds after the user types in the textbox, before the drop-down appears

maxEntries

The maximum number of entries to display in the drop-down

CSS elements

#dropdown

The dropdown element id

.dropdown_entry

Class for all entries in the dropdown

.dropdown_entry_highlighted

Class for the selected entry in the dropdown

Sample CSS

#dropdown {
    border: 1px solid #999;
    background-color: #fff;
    font-family: Helvetica, Arial, sans-serif;
    min-width: 160px;
}
.dropdown_entry {
    font-size: 12px;
    padding: 2px;
}
.dropdown_entry_highlighted {
    background-color: #36f;
    color: #fff;
}