A More Complex Way of Building Replacement Combo Boxes
In part one we looked at an easy way to recreate the functionality of combo boxes, while also creating the ability to style them effectively across browsers. In this part, we're going to work on getting rid of the hard coding of option values in the event handlers for the options, and look at dealing with combo boxes that have more options. You'll need the source files from part one to build on, and your trusty text editor at your side.
A More Complex Way of Building Replacement Combo Boxes - Increasing our Options (Page 2 of 4 )
In this example, there are just ten options, and it is easy enough to include them directly in the page while still being entirely visible, even on displays with low resolution. Many more options, though, and the options div would become taller than the display. One thing we can do is break the combo div down into smaller divs, and allow the user to access them as necessary. Add the code in bold below to the replacementcombos.html file:
So now we have two combo divs and an extra a element at the bottom of the first div. We'll need some additional CSS for the second div, and I've adjusted each div so that it is big enough to hold all 11 options without a scroll bar. Alter the combo.css file so that the selectors that match the option divs are as follows. I've highlighted the changes in bold to make them obvious:
Finally, the combo.js file will need an extra line of code in the setOption() function to close both option divs, as well as a new function to handle a click on the more options > option:
function setOption(element) { var selection = element.childNodes[0].nodeValue; var combo = document.getElementById('combofield').value = selection; document.getElementById('combodiv').style.display = "none"; document.getElementById('combodiv2').style.display = "none"; comboopenflag = "off"; }
function openNextPage() { document.getElementById('combodiv2').style.display = "block"; }