www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 01-25-2008, 12:04 PM
    adalby adalby is offline
    Registered User
     
    Join Date: Nov 2005
    Posts: 172
    Drop Down Box or Add in text option

    I'm looking for a javascript that can do both on a drop down list of either select an option or type a new drop down option in.

    Has anyone seen a script like this?
    Reply With Quote
      #2  
    Old 01-25-2008, 04:02 PM
    plepper plepper is offline
    Registered User
     
    Join Date: May 2005
    Posts: 6
    Re: Drop Down Box or Add in text option

    try this:

    Code:
    <select id="myselect" onchange="checkAddNew(this);">
    <option value="Bob">Bob</option>
    <option value="Sue">Sue</option>
    <option value="--new--">Add New User</option>
    </select>
    <input type="text" id="newOptionInput" style="display:none" onblur="addNewOption(this);">
    
    <script language="JavaScript">
    <!--
    //
    // this function is called when the user selects something from the dropdown
    //
    function checkAddNew(ele)
    {
      if (ele.value == '--new--') {
        //
        // if they chose to create a new entry in the dropdown
        //
        // grab our hidden input
        var inp = document.getElementById('newOptionInput');
        if (inp) {
          // and make it visible
          inp.style.display = 'inline';
          // and set the user's focus to it
          inp.focus();
        }
      }
    }
    
    //
    // this function is called when the user has finished typing in the new option textbox
    //
    function addNewOption(ele)
    {
      if (ele.value.length > 0) {
        //
        // if they typed anything
        //
        // get the selectbox that we're adding the option to
        var sel = document.getElementById('myselect');
        if (sel) {
          // remove the --new-- option from the selectbox
          sel.options[sel.options.length-1] = null;
          // create a new option, and set it to be the selected option
          sel.options[sel.options.length] = new Option(ele.value,ele.value,true,true);
          // recreate the add new option
          sel.options[sel.options.length] = new Option('Add New User','--new--');
        }
      }
      // clear the textbox so that it's blank the next time they try to do this
      ele.value='';
      // and hide it until we need it again
      ele.style.display = 'none';
    }
    //-->
    </script>
    if you don't care about letting them add multiple, drop the "new Option('Add New User'..." line.

    if you want to be a little more clean, instead of setting [options.length-1]=null, save it off and re-add it after you've created your new option from what they typed.

    also, you can do duplicate checking, you can loop through the sel.options array to make sure that what they just typed isn't already in the dropdown.
    Reply With Quote
      #3  
    Old 01-25-2008, 04:42 PM
    adalby adalby is offline
    Registered User
     
    Join Date: Nov 2005
    Posts: 172
    Thanks plepper this is perfect! Thanks for the help!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:15 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.