0

If you have a form consisting of a multi-select of say 50 options followed by a text-box, holding the ctrl key is the way we normal select multiples, but sometimes your 32 clicks in and well things happen... Now you've selected one or none. So, what I want to know is if it is possible to create a checkbox that when checked all clicks within a specific select field are treated as if ctrl is being held down when left click occurs.

<form action="" method="post">
<input type="checkbox" name="marker" value="1"> Click here to select multiple<br>
 <select multiple style="width:50%" name="employees[]">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
            |
            |
            |
            v
       50 more here
</select> <br>
    Your message here:<br>
    <textarea name="msg" style="width:50%"></textarea><br>
    <input name="submit" type="submit" value="submit" />
    </form>
NateW
  • 2,101
  • 3
  • 28
  • 37
DMSJax
  • 1,709
  • 4
  • 22
  • 35
  • so you want a select all option? – Daniel A. White Mar 10 '15 at 16:01
  • I think you want something like this: http://stackoverflow.com/questions/8641729/how-to-avoid-the-need-for-ctrl-click-in-a-multi-select-box-using-javascript (this is the always-on version, but this behaviour could be easily controlled by a checkbox) – thmshd Mar 10 '15 at 16:04
  • 1
    a multi-select where you can select 50 options seems to be a really bad idea – Pete Mar 10 '15 at 16:04
  • 1
    probably better to replace the whole thing with a series of checkboxes; ever try to +click on an iPad? – dandavis Mar 10 '15 at 16:05
  • The select is dynamically built based on a employees table and is an intranet, they may need to select 1 employee or 15 individuals for the form to interact with (send the message to) - I don't know how many employees there will be (1 to 3k....) I thought about the checkbox option but it gets unruley in large numbers – DMSJax Mar 10 '15 at 16:06
  • @DanielA.White No, not a select all - just the ability to select multiples without holding the CTRL key down for that form element by filling a checkbox to indicate CTRL should be simulated on each click – DMSJax Mar 10 '15 at 16:08
  • checkboxes get unruley as fast as selects, and if you're on mobile, – dandavis Mar 10 '15 at 16:14
  • @dandavis select2 looks promising, that might be the way i go after i play with it more. Thanks for that suggestion. – DMSJax Mar 10 '15 at 16:23

2 Answers2

1

With JQuery you can easily manipulate <select>, see .val() function. With it you can know what are selected and you can add what you want to the selection.

Oneil67
  • 98
  • 8
0

This is a possible script solution. Based on this solution

$('option').mousedown(function(e) {
    if ($('#marker').is(":checked")) {
        e.preventDefault();
        $(this).prop('selected', !$(this).prop('selected'));
        return false;
    }
});
Community
  • 1
  • 1
thmshd
  • 5,729
  • 3
  • 39
  • 67
  • See the updated version. http://jsfiddle.net/wzrfvcrz/2/ (you also put the JS in the CSS field and i had to made minor changes). But if you read the original article, the behaviour can appear a little bit... "buggy". Maybe you can find workarounds if it appears to be buggy in your browser. – thmshd Mar 10 '15 at 16:30