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?
– bfavaretto
Do you have a multipleselect (jQuery plugin) or are you making your own? you can add HTML?
– Sergio
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.
– Joao Paulo
I’ll break my head a little more and if I can get the result here.
– Joao Paulo
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 :)
– Onilton Maciel