Style Sheets
  Home arrow Style Sheets arrow Page 4 - An Easy Way to Build Replacement Combo Box...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

An Easy Way to Build Replacement Combo Boxes
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2007-04-09

    Table of Contents:
  • An Easy Way to Build Replacement Combo Boxes
  • Adding Some CSS
  • Styling the Text Input Field
  • Adding JavaScript

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    An Easy Way to Build Replacement Combo Boxes - Adding JavaScript


    (Page 4 of 4 )

    Finally, we need to add a little basic JavaScript to control the opening and closing of the options div, and to add the selection made by the visitor to the text field. In a new file in your text editor begin with the following:

    var comboopenflag;
     
    function init() {
     
    var combo = document.getElementById('combofield').value = "";
     
    comboopenflag = "off";
    }

    This is called on the onload event of the body of the page. It ensures the options div is not open and sets the flag appropriately. This is needed after, say, a page refresh (it's not likely halfway through filling out a form, but it can happen). Our next function will open the div, or close it if it is already open and is called from the onclick event of the image used as the drop-down arrow:

    function showOptions() {
     
    if (comboopenflag != "on") {
       
    document.getElementById('combodiv').style.display = "block";
       
    comboopenflag = "on";
     
    } else if (comboopenflag = "on") {
       
    document.getElementById('combodiv').style.display = "none";
       
    comboopenflag = "off";
     
    }
    }

    This uses the flag to determine whether or not the div is currently open, and reverses its state, so when it is closed, it gets opened, and when it is opened it get closed. Finally, we need to get the selected option into the text field:

    function setOption(selection) {
     
    var combo = document.getElementById('combofield').value = selection;
     
    document.getElementById('combodiv').style.display = "none";
     
    comboopenflag = "off";
    }

    This accepts the parameter sent by the onclick event in the HTML file and uses this to add its value to the text field. Simple. It also closes off the options div. Save the file as combo.js in the same location as the HTML and CSS files.

    If you view the page in either IE or FireFox, the box should function exactly as a combo box would, and work consistently across the two major vendors. I've only used very basic styling here to illustrate the point, but I'm sure you can see the potential of this technique. As I mentioned at the start of the article, this is the easy way to do it and is suitable only for small combo boxes with few options.

    Hard-coding the options into the onclick event call has its limitations. In the next article, I'll be looking at a method that is better put to use with larger combo boxes with many options as well as a way of getting rid of the hard-coded values.


    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.

       · Hello All,This article shows a very quick and easy way to implement custom...
       · HiIs it my imagination or is there no example of what this replacement combo box...
       · Hi, I totally appreciate your point, however I have no control over the CMS.I...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek