I'm trying to make a script to select multiple options
of a <select multiple>
with each mouse click. So far so good, I've 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 that by clicking on an already selected it deselected.
My algorithm is pretty bad and it gives a 'blink' effect every time I click on a new option
, ie it shows a just selected at the time of the click and then add the others that have already been clicked previously, that was not cool either.
Does anyone have any idea how to increment the unselect option in my code, or show more efficient code?
NOTE: I DO NOT WANT TO USE ANY PLUGIN.