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 08-12-2006, 05:08 PM
    gert cuykens gert cuykens is offline
    Registered User
     
    Join Date: Oct 2004
    Posts: 564
    select.add(v)

    How does select.add() work ?

    Error: uncaught exception: [Exception... "Not enough arguments [nsIDOMHTMLSelectElement.add]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://localhost/gert/db4/js/html.js :: op :: line 23" data: no]

    select.add(v)

    Error: uncaught exception: [Exception... "Could not convert JavaScript argument - 0 was passed, expected object. Did you mean null? arg 0 [nsIDOMHTMLSelectElement.add]" nsresult: "0x80570035 (NS_ERROR_XPC_BAD_CONVERT_JS_ZERO_ISNOT_NULL)" location: "JS frame :: http://localhost/gert/db4/js/html.js :: op :: line 23" data: no]

    select.add(0,v)
    Reply With Quote
      #2  
    Old 08-12-2006, 05:55 PM
    gert cuykens gert cuykens is offline
    Registered User
     
    Join Date: Oct 2004
    Posts: 564
    function op(n,v)
    {
    i = document.getElementsByName(n)[0].options.length
    option = document.createElement('option')
    option.value = v
    option.text = v
    document.getElementsByName(n)[0].add(option,i)
    }

    also doesnt work
    Reply With Quote
      #3  
    Old 08-12-2006, 06:00 PM
    gert cuykens gert cuykens is offline
    Registered User
     
    Join Date: Oct 2004
    Posts: 564
    function op(n,v)
    {
    i = document.getElementsByName(n)[0].options.length
    option = document.createElement('option')
    option.value = v
    option.text = v
    document.getElementsByName(n)[0].options.add(option,i)
    }

    But this does
    Reply With Quote
      #4  
    Old 09-03-2006, 07:33 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,827
    A few notes.
    1. Should really be using: new Option("text","value") not createElement and spam 3 extra lines.
    2. getElementsByName is glitchy in IE.
    3. If you want to append a new OPTION to the end of a SELECT dropdown menu, instead of counting all the OPTIONS there are and sending that number as a 2nd argument to the "add" method, just send it null instead.

    Code:
    function op(n,v)
    {
    document.getElementsByName(n)[0].options.add(new Option(v,v),null);
    }
    However remember what I said about getElementsByName being glitchy in IE.
    Besides, it's better to make sure the element actually exists before calling the function and instead sending it a reference.
    Code:
    function op(e,v)
    {
    e.options.add(new Option(v,v),null);
    }
    Seeing the code is so short now, maybe it's better to just avoid the function altogether.

    Then again, I tend to avoid the "add" and "remove" methods myself.
    Code:
    e.options[e.options.length]=new Option("text","value");
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.

    Last edited by Ultimater; 09-03-2006 at 07:35 AM.
    Reply With Quote
      #5  
    Old 09-03-2006, 08:33 AM
    gert cuykens gert cuykens is offline
    Registered User
     
    Join Date: Oct 2004
    Posts: 564
    Makes me wonder how many other spam lines i have in my code lol
    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 01:57 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.