Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Fastest way to select all elements in a *ListBox*

Fastest way to select all elements in a *ListBox*

Scheduled Pinned Locked Moved JavaScript
javascriptcomtoolsquestionlearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    A
    wrote on last edited by
    #1

    I am trying to select all the elements in a Listbox/SelectBox. At the moment I am using the following function *which is working*, the only thing is it takes around 10 seconds to select all elements(of which there are 652). I'm a beginner at javascript so I was wondering whether anyone knew of a faster way to select all elements.

    //selectBox is the selectbox ID, selectall is a boolean whether to select all elements,
    //ensureOneSelected applies when false is specified for selectall
    //it then ensures that the first element is always selected.

    function selectAll(selectBox, selectAll, ensureOneSelected) {
    // have we been passed an ID
    var selectBoxElement = null;
    var selectBoxOption = null;

    var boolSelect = selectAll;
    
    if (typeof selectBox == "string") {
        selectBoxElement = document.getElementById(selectBox);
        selectBoxOption = selectBoxElement.options;
    }
    // is the select box a multiple select box?
    if (selectBoxElement.type == "select-multiple") {
        var max = (selectBoxOption.length);
    
        var modMaxUnrollFactor = (max % 4);
    
        max -= modMaxUnrollFactor ;
    
        for(var i = 0; i < max; i+=4){
            selectBoxOption\[i\].selected = boolSelect;
            selectBoxOption\[i + 1\].selected = boolSelect;
            selectBoxOption\[i + 2\].selected = boolSelect;
            selectBoxOption\[i + 3\].selected = boolSelect;
    
        }
        for (var i = max; i < selectBoxOption.length; i++) {
            selectBoxOption\[i\].selected = boolSelect;
        }
    
        if (typeof ensureOneSelected == "boolean") {
            if(selectBoxOption\[0\].selected == false)
            {
                selectBoxOption\[0\].selected = ensureOneSelected;
            }
        }
    }
    

    }

    thanks in advance

    My blog:[^]

    B 1 Reply Last reply
    0
    • A A

      I am trying to select all the elements in a Listbox/SelectBox. At the moment I am using the following function *which is working*, the only thing is it takes around 10 seconds to select all elements(of which there are 652). I'm a beginner at javascript so I was wondering whether anyone knew of a faster way to select all elements.

      //selectBox is the selectbox ID, selectall is a boolean whether to select all elements,
      //ensureOneSelected applies when false is specified for selectall
      //it then ensures that the first element is always selected.

      function selectAll(selectBox, selectAll, ensureOneSelected) {
      // have we been passed an ID
      var selectBoxElement = null;
      var selectBoxOption = null;

      var boolSelect = selectAll;
      
      if (typeof selectBox == "string") {
          selectBoxElement = document.getElementById(selectBox);
          selectBoxOption = selectBoxElement.options;
      }
      // is the select box a multiple select box?
      if (selectBoxElement.type == "select-multiple") {
          var max = (selectBoxOption.length);
      
          var modMaxUnrollFactor = (max % 4);
      
          max -= modMaxUnrollFactor ;
      
          for(var i = 0; i < max; i+=4){
              selectBoxOption\[i\].selected = boolSelect;
              selectBoxOption\[i + 1\].selected = boolSelect;
              selectBoxOption\[i + 2\].selected = boolSelect;
              selectBoxOption\[i + 3\].selected = boolSelect;
      
          }
          for (var i = max; i < selectBoxOption.length; i++) {
              selectBoxOption\[i\].selected = boolSelect;
          }
      
          if (typeof ensureOneSelected == "boolean") {
              if(selectBoxOption\[0\].selected == false)
              {
                  selectBoxOption\[0\].selected = ensureOneSelected;
              }
          }
      }
      

      }

      thanks in advance

      My blog:[^]

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      What is with the unroll factor and two loops? That is pointless ... running a for loop 600 times isn't what's slow. Manipulating the DOM can be slow. You might do better to get .innerHTML, do a regex replace for '<option( selected)?' with either '<option' or '<option selected', and then reassign .innerHTML.

      A 2 Replies Last reply
      0
      • B BobJanova

        What is with the unroll factor and two loops? That is pointless ... running a for loop 600 times isn't what's slow. Manipulating the DOM can be slow. You might do better to get .innerHTML, do a regex replace for '<option( selected)?' with either '<option' or '<option selected', and then reassign .innerHTML.

        A Offline
        A Offline
        A
        wrote on last edited by
        #3

        BobJanova wrote:

        Manipulating the DOM can be slow. You might do better to get .innerHTML, do a regex replace for '<option( selected)?' with either '<option' or '<option selected', and then reassign .innerHTML.

        Thanks, I'll look into it.

        BobJanova wrote:

        What is with the unroll factor and two loops? That is pointless ... running a for loop 600 times isn't what's slow.

        I'm a c/c++ programmer predominately, force of habit :)

        My blog:[^]

        1 Reply Last reply
        0
        • B BobJanova

          What is with the unroll factor and two loops? That is pointless ... running a for loop 600 times isn't what's slow. Manipulating the DOM can be slow. You might do better to get .innerHTML, do a regex replace for '<option( selected)?' with either '<option' or '<option selected', and then reassign .innerHTML.

          A Offline
          A Offline
          A
          wrote on last edited by
          #4

          Developed a working solution on IE8 though I did run into a select option truncate bug where the first option in innerHTML is removed. :| But in the end I developed a workaround for this bug and have a working hack solution which is much much faster, so all is good. :) cheers.:thumbsup:

          My blog:[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups