23

I've come across various solutions to this issue on the net.

Basically, I find having to hold down ctrl a bit cheesy, and I want the select list to just select whatever I click on and add it to the currently selected items.

I've already got this code:

    $("select[multiple] option").mousedown(function () {
        var $self = $(this);            

        if ($self.attr("selected")) {                
            $self.removeAttr("selected", "");
        }
        else {
            $self.attr("selected", "selected");
        }

        return false;            
    });

Element:

    <select multiple id="WOStatusKey" name="WOStatusKey">                
        <option value="1">Created</option>
        <option value="2">In Process</option>
        <!-- etc. (about 20 of these) -->
    </select>

It works fine with one exception: any time something is selected/deselected that is not at the top of the list (where you have to scroll to see it) then it pops back up to the top after you select it. I've played around with it a little bit but can't figure out anything to prevent this behavior. Also, I've seen a couple other solutions to this problem, although nothing that either works or works well.

I only need this to work in Chrome. Also, I am not interested in any alternative solutions to using a select list.

Thank you for any help, it is much appreciated.

3
  • Can you make a fiddle for this so others could also experiment with your code? Commented Jul 3, 2014 at 2:10
  • What browser? Not seeing the problem in chrome Commented Jul 3, 2014 at 2:12
  • janaspage: on your link, scroll down and click on an option. After selection, the select box will automatically reset the position to be at the very top (as if you scrolled all the way back up). aa333: jsfiddle.net/h5Sw2 Commented Jul 3, 2014 at 18:51

8 Answers 8

47

You can save the Element.scrollTop and set it at the end.

$("select").mousedown(function(e){
    e.preventDefault();

    var select = this;
    var scroll = select .scrollTop;

    e.target.selected = !e.target.selected;

    setTimeout(function(){select.scrollTop = scroll;}, 0);

    $(select ).focus();
}).mousemove(function(e){e.preventDefault()});

http://jsfiddle.net/UziTech/cjjg68dr/114/

Sign up to request clarification or add additional context in comments.

14 Comments

Thank you sir! I never did find a solution. This works great!
Finally a solution that works! btw, tested it on Safari/Opera/Chrome/FF, doesn't work for FF
This fiddle doesn't work in the latest Chrome; as the OP described, if you select, say #2, and then scroll down to #9, the select box will auto scroll to #2 again immediately after click.
I updated the fiddle to set the scrollTop inside a setTimeout so it will update it at the end of the event loop
If you have onChange handlers that preventDefault() is preventing from working, just add $("#mySelect").trigger("change"). You'll still get the Ctrl+click effect.
|
8

Tony's answer makes the select arrows buggy, as they work only if you hold the mouse down.

I've combined a few solutions into this, and it works fine at least in Chrome and FF:

// multiple select: selectable without Control
$('select[multiple] option').on('mousedown', function(e) {
    var $this = $(this),
        that = this,
        scroll = that.parentElement.scrollTop;

    e.preventDefault();
    $this.prop('selected', !$this.prop('selected'));

    setTimeout(function() {
        that.parentElement.scrollTop = scroll;
    }, 0);

    return false;
});

1 Comment

only answer that works on chrome mobile cause it targets the option element
7

Here is a pure JS solution:

element.onmousedown= function(event) {
    //this == event.target
    event.preventDefault();
    var scroll_offset= this.parentElement.scrollTop;
    this.selected= !this.selected;
    this.parentElement.scrollTop= scroll_offset;
}
element.onmousemove= function(event) {
    event.preventDefault();
}

Look at the parent element(the select box) and record the vertical scroll offset before selecting/deselecting the option. Then reset it manually once you have performed the action.

The reasoning behind preventing default behavior for the mousemove event is because if you don't prevent it and you happen to click an option while moving the mouse, all options will become de-selected.

1 Comment

This works fine. A side effect is that other events may not fire. After adding this, I observed the following: (a) a disabled multiselect select field accepted the selection of options when clicking at an option. (b) An event that has been registered at 'input' did not fire any more.
2

Short quick version

Created In Process

// this is for specyfic id  -> $('#WOStatusKey').

//below for all multiple select
$('select[multiple]').mousedown(e=>{
e.target.selected = !e.target.selected;
e.stopPropagation();
return false;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select multiple id="WOStatusKey" name="WOStatusKey">                
        <option value="1">Created</option>
        <option value="2">In Process</option>
        <option value="3">Bla bla bla</option>
    </select>

2 Comments

Would be even better if it displayed a ☑️ when option is selected and a 🔳 when it's not
I made a version of this in vanilla js and with checkboxes stackoverflow.com/a/78177261/3374574
1

Here's a solution that appears to work in Chrome FF and IE. It's not terribly pretty because it flashes a little bit when clicking.

var vals = [];
$("select[multiple]").click(function(e){
        var scroll_offset= this.scrollTop;
      var newVals = $(this).val();
    if (newVals.length === 1) {
        var index = vals.indexOf(newVals[0])
        if (index > -1) {
        vals.splice(index, 1);
      } else {
        vals.push(newVals[0])
      }
      $(this).val(vals);
      this.scrollTop = scroll_offset;
    }
});

jsfiddle

Comments

1

For a pure js solution in chrome set the event listener on "onmouseup" instead. The event that resets the scroll to the first element is fired on the mouse up. So for example:

element.onmouseup = function(event) {
    event.preventDefault();
    var st = this.scrollTop;
    setTimeout(function () { element.scrollTop = st; }, 0);
};

Comments

1

This version allows users to see a checked checkbox when the option is selected and an unchecked checkbox when the option is not selected on a desktop device. Mobile device already have checkboxes for select multiple.

native implementation of select multiple with checkboxes

const init = function () {
// native mobile select has already checkboxes on both ios and android
  if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
    return;
  }

  document.querySelectorAll('select[multiple][checkboxes] option').forEach(option => {
    if (option.selected) {
      option.textContent = '☑️ ' + option.textContent;
    } else {
      option.textContent = '🔳 ' + option.textContent;
    }
  });

  document.querySelector('select[multiple][checkboxes]').addEventListener('mousedown', function (e) {
    e.preventDefault();
    const initialPosition = e.target.parentElement.scrollTop;
    if (e.target.tagName !== 'OPTION') return;
    e.target.selected = !e.target.selected;
    if (e.target.selected) {
      e.target.textContent = e.target.textContent.replace('🔳', '☑️');
    } else if (!e.target.selected) {
      e.target.textContent = e.target.textContent.replace('☑️', '🔳');
    }
    setTimeout(() => {
      e.target.parentElement.scrollTop = initialPosition;
    }, 0);
    e.target.dispatchEvent(new Event('change'));
    return false;

  });
}

init();
<select multiple checkboxes>
  <option value="1" selected>Todo</option>
  <option value="2">In Process</option>
  <option value="3">Done</option>
  <option value="4">Bla bla bla</option>
  <option value="5">Bla bla bla</option>
  <option value="6">Bla bla bla</option>
  <option value="7">Bla bla bla</option>
  <option value="8">Bla bla bla</option>
  <option value="9">Bla bla bla</option>
  <option value="10">Bla bla bla</option>
  <option value="11">Bla bla bla</option>
  <option value="12">Bla bla bla</option>
  <option value="13">Bla bla bla</option>
</select>

VS

<select multiple>
  <option value="1" selected>Todo</option>
  <option value="2">In Process</option>
  <option value="3">Done</option>
  <option value="4">Bla bla bla</option>
  <option value="5">Bla bla bla</option>
  <option value="6">Bla bla bla</option>
  <option value="7">Bla bla bla</option>
  <option value="8">Bla bla bla</option>
  <option value="9">Bla bla bla</option>
  <option value="10">Bla bla bla</option>
  <option value="11">Bla bla bla</option>
  <option value="12">Bla bla bla</option>
  <option value="13">Bla bla bla</option>
</select>

Comments

0

This worked better for me than the other suggestions, because it is vanilla JS and it behaves more similarly to the original still, for example allows quick selection of many items via the shift key.

element.onmousedown = function(event) {
    if (event.shiftKey) return;
    event.preventDefault();
    this.focus();
    var scroll = this.scrollTop;
    event.target.selected = !event.target.selected;
    this.scrollTop = scroll;
    // make sure that other listeners (like Vue) pick up on the change
    // even though we prevented the default.
    this.dispatchEvent(new Event("change"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.