How to select/deselect an input select Multiple at each mouse click? (without Ctrl)

Asked

Viewed 1,856 times

5

I’m trying to make a script to select several options of a <select multiple> every mouse click. So far so good, I got and follows the code:

var numSelected = 0;
var valSelectedArray = [];

$(document).on('mouseup', '#CentroCusto_new option', function () {
    var valSelected = $(this).val();
    valSelectedArray[numSelected] = valSelected;
    numSelected++;

    for (i = 0; i < numSelected; i++) {
            $("#CentroCusto_new option[value=" + valSelectedArray[i] + "]").prop("selected", true);
    }
});

However, I would like to click on an already selected one to desiccate.

My algorithm is really bad and it gives a 'blink' effect every time I click on a new one option, that is, it shows a just selected at the time of the click to then add the others that were already clicked previously, this also was not cool.

Does anyone have any idea how to increment the option to deselect in my code, or else show a more efficient code?

NOTE: I DO NOT WANT TO USE ANY PLUGIN.

  • You don’t want normal selection mode with CTRL, that’s it?

  • Do you have a multipleselect (jQuery plugin) or are you making your own? you can add HTML?

  • Yes, I don’t want the user to have to use Ctrl. html is not necessary because it is simple and with dynamic content, id is #Centrocusto_new. This post is about a plugin, I had already entered it, but I want my own.

  • I’ll break my head a little more and if I can get the result here.

  • It would be nice to add to the question title, and make it very clear, that is without Ctrl, something like (without Ctrl). Probably this question will be used by people in the future :)

2 answers

4


Try this:

$('select#CentroCusto_new option').on('mousedown', function (event) {
    this.selected = !this.selected;
    event.preventDefault();
});

With this, as soon as you press the mouse button (mousedown) above the element, the option Selected receives the opposite of the previous state of the same.

The event.preventDefault() causes the standard action associated with that event not to be triggered/activated.

For example, if the event was about a tag <a href="http://testlink.com">, you would not be directed to a new Url.

In our case, the preventDefault causes the (default) action of unchecking all other entries when you click on an item (without Ctrl) not to be executed. Note that disabling the default action also causes the item to not be automatically selected, so the need for the this.selected = !this.selected;

More information on the preventDefault here.

Proof of concept:

http://jsfiddle.net/RkJS7/2/

  • Perfect! Could you give a brief explanation about preventDefault? and the parameter "and" what it is?

  • 1

    @Joaopaulo I added the explanations you asked for :)

-1

John Paul, by default a <select multiple> can have multiple elements selected or deselected using Ctrl + click, and also dragging the mouse click.

  • 1

    Thanks for the @Paulo Roberto edition, the <kbd> I really didn’t know

  • Provide, if you want to know more: http://meta.stackexchange.com/questions/1777/what-html-tags-are-allowed-stack-exchange-sites and also http://answall.com/editing-help

  • 1

    Very good, there are several things that I did not know what to do, I really liked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.