Automate CTRL to allow multi-select

For a program that I am writing, I want to include a button on one of the UI's that when you click it, it automates holding down the CTRL key, when they click the button again, it will turn off the CTRL.  I will be using it to enable the user to easily select multiple items in a list.  
 Any suggestions? Thank you.
CaitlinRobertsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

noamattdCommented:
I don't think this is possible.  

I would suggest you change your multiple select list to a list of checkboxes, as you can do more with styling the elements.  You can put them in their own div tag and use some CSS to give it a border & vertical scrollbar to make it look more like a select.
dbrittCommented:
Caitlin, I'm working on it...don't give up :)
KennyTMCommented:
Try this...

<script>
function change_sel (obj) {
      if (!this.selected_array)
            this.selected_array = new Array();
      for (var i = 0; i < obj.options.length; ++ i) {
            if (this.selected_array[i] == undefined)
                  this.selected_array[i] = false;
            if (obj.options[i].selected)
                  obj.options[i].selected = selected_array[i] = !selected_array[i];
            else
                  obj.options[i].selected = selected_array[i];
      }
}
</script>


<select multiple id="xxx" onchange="change_sel(this);">
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
<option>e</option>
<option>f</option>
<option>g</option>
</select>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

KennyTMCommented:
BTW, the code doesn't really help you hold down the CTRL key. It just reloads previous state of the "irrelevent" options. This introduce a little flash during selection...

(If you want to select a single option, hold the CTRL key ;) )
dbrittCommented:
It doesn't work when you try to drag-select...
dbrittCommented:
You can keep Kenny's as the answer, I just put enough effort into this script that I feel obligated to post it :)
Pretty much the only difference is that you can toggle between CTRL modes and also you can deselect an option when it is the only one in the select (can't do that w/ Kenny's). I was looking for a way to get the dragging to work perfectly, but it looks like Kenny's does it just as well as can be done. ^^
I personally like Kenny's version for it's conciseness of code, maybe you can find a way to munge his with my way of deselecting a lonely option.

=================================


<html>
<head>
      <title>Page</title>

      <script language="JavaScript">

      var ctrlEnabled = true;
      var selects = new Array();

      function toggleCtrl()
      {
            ctrlEnabled = !ctrlEnabled;

            for(var i = 0; i < selects.length; i++)
            {
                  selects[i].refreshed = true;
            }

            // I just added this to change the text on the button, you can remove it
            window.event.srcElement.value = "Turn CTRL " + (ctrlEnabled ? "off" : "on");
      }

      function saveSelection(sel)
      {
            if(!ctrlEnabled || (window.event && window.event.ctrlKey))
                  return;

            if(!selects[sel.id])
            {
                  selects[sel.id] = new Object();
            }

            var curr = selects[sel.id];

            curr.options = new Array();

            for(var i = 0; i < sel.options.length; i++)
            {
                  var option = new Object();
                  option.selected = sel.options[i].selected;
                  
                  curr.options.push(option);
            }

            sel.selectedIndex = -1;
      }

      function multiSelect(sel)
      {
            if(window.event && window.event.ctrlKey)
                  return;

            if(ctrlEnabled)
            {
                  var curr = selects[sel.id];
                  
                  for(var i = 0; i < sel.options.length; i++)
                  {
                        sel.options[i].selected = sel.options[i].selected ^ curr.options[i].selected;
                  }
            }
      }

      </script>
</head>
<body>

<input type="button" value="Turn CTRL off" onclick="javascript:toggleCtrl();">

<br><br>

<select id="sel1" size="6" multiple="true" onmousedown="javascript:saveSelection(this);" onchange="javascript:multiSelect(this);">

<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
<option>e</option>
<option>f</option>

</select>

<select id="sel1" size="6" multiple="true" onmousedown="javascript:saveSelection(this);" onchange="javascript:multiSelect(this);">

<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>

</select>

</body>
</html>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.