If you find this function really handy, and want to reuse it over and over, don't fall into the trap of duplicating code. The best way to handle that is to pass in the select menus you're operating on via the argument list, and that way the same function can be used on lots of different menus throughout your application.
I have one big recommendation for this script. When coding it into your page, put the code after the </html> tag. Not necessarily the entire function, but at least the array generating code. The reason for this is that the array can become quite large, and you don't want the generation thereof to slow down the loading of the rest of your page.
I hope you enjoy this little tool. Speaking from experience – once you implement it, you won't be able to live without it. If you have problems, feel free to hop on the forums! By the way, I've tested it with all major current browsers, and have not encountered a single problem.
<script language="JavaScript">
var arItems = new Array()
arItems = [
<%
strSQL = "SELECT type_id, id, description FROM items"
objRS.Open strSQL, strConn
arItems = objRS.GetRows()
objRS.Close()
for i = 0 to uBound( atItems, 2 )
response.Write( "[" & atItems( 0, i ) & "," & atItems( 1, i ) & _
",'" & atItems( 2, i ) & "']" )
if i < uBound( atItems, 2 ) then response.Write("," & vbCrLf )
next
%>
]
function fillItems( intStart ) {
var fTypes = document.form1.types
var fItems = document.form1.items
var a = atItems
var b, c, d, intItem, intType
if ( intStart > 0 ) {
for ( b = 0; b < a.length; b++ ) {
if ( a[b][1] == intStart ) {
intType = a[b][0];
}
}
for ( c = 0; c < fTypes.length; c++ ) {
if ( fTypes.options[ c ].value == intType ) {
fTypes.selectedIndex = c;
}
}
}
if ( intType == null ) {
intType = fTypes.options[ fTypes.selectedIndex ].value
}
fItems.options.length = 0;
for ( d = 0; d < a.length; d++ ) {
if ( a[d][0] == intType ) {
fItems.options[ fItems.options.length ] = new Option( a[d][2], a[d][1] ); // no line-break here
}
if ( a[d][1] == intStart ) {
fItems.selectedIndex = fItems.options.length - 1;
}
}
}
</script>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |