Removing all but first item in drop down list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    Removing all but first item in drop down list

    How do I amend the following code to skip removing the first option in
    my dropdown list i.e. I want the first option "Please select..." to
    stay?

    while (document.getEl ementById("myco mbo").childNode s.length 0) {
    document.getEle mentById("mycom bo").removeChil d(document.getE lementById("myc ombo").childNod es[0])
    }

    Many thanks,

    Chris
  • Gregor Kofler

    #2
    Re: Removing all but first item in drop down list

    Chris meinte:
    How do I amend the following code to skip removing the first option in
    my dropdown list i.e. I want the first option "Please select..." to
    stay?
    >
    Untested:

    var e = document.getEle mentById("mycom bo");
    while (e.childNodes.l ength 1) {
    e.removeChild(e .lastChild);
    }

    Gregor

    Comment

    • liamgegan@gmail.com

      #3
      Re: Removing all but first item in drop down list

      var oplist=document .getElementById ("mycombo");
      for(var i = oplist.options. length-1;i>0;i--)
      {
      oplist.removeCh ild(oplist.opti ons[i]);
      }

      Comment

      • Conrad Lender

        #4
        Re: Removing all but first item in drop down list

        On 2008-10-23 22:58, Chris wrote:
        How do I amend the following code to skip removing the first option
        in my dropdown list i.e. I want the first option "Please select..."
        to stay?
        >
        while (document.getEl ementById("myco mbo").childNode s.length 0) {
        document.getEle mentById("mycom bo").removeChil d(document.getE lementById("myc ombo").childNod es[0])
        }
        document.getEle mentById("mycom bo").options.le ngth = 1;


        - Conrad

        Comment

        • Jorge

          #5
          Re: Removing all but first item in drop down list

          On Oct 23, 10:58 pm, Chris <matchett...@go oglemail.comwro te:
          How do I amend the following code to skip removing the first option in
          my dropdown list i.e. I want the first option "Please select..." to
          stay?
          >
          while (document.getEl ementById("myco mbo").childNode s.length 0) {
          document.getEle mentById("mycom bo").removeChil d(document.getE lementById("myc ombo").childNod es[0])
          }
          >
          var e= document.getEle mentById("mycom bo");
          while (e.firstChild !== (d= e.lastChild)) { e.removeChild(d ); }


          If there are many children this might be faster:

          var e= document.getEle mentById("mycom bo"), d= e.firstChild;
          e.innerHTML= "";
          e.appendChild(d );

          --
          Jorge.

          Comment

          • Amrit Ranjan

            #6
            Re: Removing all but first item in drop down list

            I think
            document.getEle mentById("mycom bo").length = 1;
            will be more than enough.

            Comment

            • Amrit Ranjan

              #7
              Re: Removing all but first item in drop down list

              I think
              document.getEle mentById("mycom bo").length = 1;
              will be more than enough.

              Comment

              • Chris

                #8
                Re: Removing all but first item in drop down list

                On 24 Oct, 08:21, Amrit Ranjan <amri...@gmail. comwrote:
                I think
                            document.getEle mentById("mycom bo").length = 1;
                will be more than enough.
                Didn't get it at first Amrit but thanks, very clever!

                Chris

                Comment

                • Gregor Kofler

                  #9
                  Re: Removing all but first item in drop down list

                  Conrad Lender meinte:
                  On 2008-10-23 22:58, Chris wrote:
                  >How do I amend the following code to skip removing the first option
                  >in my dropdown list i.e. I want the first option "Please select..."
                  >to stay?
                  >>
                  >while (document.getEl ementById("myco mbo").childNode s.length 0) {
                  >document.getEl ementById("myco mbo").removeChi ld(document.get ElementById("my combo").childNo des[0])
                  > }
                  >
                  document.getEle mentById("mycom bo").options.le ngth = 1;
                  Will this free the ressources occupied by the now discarded options, too?

                  Gregor

                  Comment

                  • Conrad Lender

                    #10
                    Re: Removing all but first item in drop down list

                    On 2008-10-25 23:10, Gregor Kofler wrote:
                    >document.getEl ementById("myco mbo").options.l ength = 1;
                    >
                    Will this free the ressources occupied by the now discarded options,
                    too?
                    I guess that would depend on the implementation. They should be freed
                    (sooner or later) by a reasonably designed scripting engine, same as
                    with removeChild(). In IE, on the other hand, there's still the GC
                    problem with circular references between JScript and host objects, so
                    there's at least one implementation that won't free the resources in
                    certain cases. removeChild() wouldn't help there either.


                    - Conrad

                    Comment

                    Working...