Results 1 to 6 of 6

Thread: Deleteing items out of select box

  1. #1

    Thread Starter
    Frenzied Member msimmons's Avatar
    Join Date
    Jul 2001
    Location
    Houston, TX
    Posts
    1,057

    Deleteing items out of select box

    I know how to dynamicly add items, but I don't know how to delete them dynamicly. One way I could do it would be to outerHTML the whole box but that would require a database call in my situation and speed is always an issue...
    is there an easy way to delete one item from a select box?
    Thanks in advance,
    Michael

  2. #2
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Code:
    <html>
      <head>
        <script type="text/javascript">
          function removeOpt(index) {
            document.getElementById("mySelect").removeChild(document.getElementById("mySelect").childNodes[index]);
          }
        </script>
      </head>
      <body>
        <form name="myForm">
          <select id="mySelect">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
          </select>
        </form>
        <div onclick="JavaScript:removeOpt(1);">Remove 1</div>
      </body>
    </html>
    It is official. I have decided that JavaScript is long winded and tempermental. And this is all because of the W3C DOM Spec and the ECMA bindings.

    Oh, this is DOM 2, by the way.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  3. #3

    Thread Starter
    Frenzied Member msimmons's Avatar
    Join Date
    Jul 2001
    Location
    Houston, TX
    Posts
    1,057
    It didn't quite work (it could be me). First I put it into my code and modified it. I get 'type mismatch' error (sugesting that the value i give it is not in the select). So I copied the code exactly (had to take out "javascript:" because it is redundant in an onClick) and it removed the options 1 at a time starting with 0 then 1 then 2.
    Is that what was intended? I needed to delete specific ones by value (index).
    Thanks,
    Michael

  4. #4

    Thread Starter
    Frenzied Member msimmons's Avatar
    Join Date
    Jul 2001
    Location
    Houston, TX
    Posts
    1,057
    i found a way:
    Code:
    function delSel() {
      o = parent.frames [0].document.all.PickHotel.options;
      for (var i=(o.length-1); i>=0; i--) {
        if (o[i].selected) {
          o[i] = null;
        }
      }
    }
    thanks,
    michael

  5. #5
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    As to "javascript" in the onClick attribute, it is habit. I don't know what the standards dictate.

    The page I posted should remove "1", and then "2" which is the new number 1, and it shouldn't do any more. It should never remove "0".

    The code you posted will not work in all browsers. "document.all" is not part of the DOM as far as I know. Admittedly the code I posted will not work in all browsers, but that is because not all browsers are up to date with the specs.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  6. #6

    Thread Starter
    Frenzied Member msimmons's Avatar
    Join Date
    Jul 2001
    Location
    Houston, TX
    Posts
    1,057
    it continues to remove 0 and tries to remove from the empty select after that.
    As far as I know 'document.all' is the same as 'document.forms["formname"]'
    Thanks
    Michael
    I'm off to GalahTech, hope to see you there.

    If you don't like the rules they make, refuse to play their game. -- Steve Ignorant.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width