1

I'm trying to change the default behavior of a so that single-clicks on its options change the selected state of those options.

In other words, I want to remove unintuitive requirement of holding shift / ctrl to make multiple selections.

I'm trying with jQuery:

jQuery('select[multiple] option').each(function() {

var $this = jQuery(this);

 if ($this.is(':selected')) {
   $this.click(function(e) {
        e.preventDefault();
        $this.selected = 'false';
   });
 } else {
   $this.click(function(e) {
        e.preventDefault();
        $this.selected = 'true';
   });
 }
});

but it's not working. Any ideas?

AT Design
  • 60
  • 2
  • 11

1 Answers1

1

Try this answer

You can't depend on "click" event, the behavior of "select" and its "option"s is a bit different, you have to use mouse-events to achieve your goal.

EDIT 2021:

Best way to deal with this is actually to use checkboxes <input type="checkbox">, because browsers (mobile+desktop) will natively handle for you that without you trying to hack around it with an ugly script.

evilReiko
  • 19,501
  • 24
  • 86
  • 102