0

EDIT: I will explain my problem better:

I have a Select Multiple created with Jquery-plugin --> Chosen. I create the Options inside the Select with Ajax+Php+MySQL and this:

$("#Enmarcado").append(array_items[x]); //inside a loop
[...]
$("#Enmarcado").trigger("chosen:updated");

Now, with the Select created, I want validate which options you can select:

This is my select (html created by Ajax+Php+MySQL)

<select id="Enmarcado" name="Enmarcado" data-placeholder="Elije los acabados..." class="chosen-select" multiple="" onchange="validar_enmarcado();">
    <option selected="" value="-1">Sin Acabados</option>
    <option value="20">Kappa-fix 10mm (PVC)</option>
    <option value="19">Kappa-fix 5mm (PVC)</option>
    <option value="18">Laminado mate (75 micras producción)</option>
    <option value="17">Laminado brillo (75 micras producción)</option>
    <option value="16">Laminado mate (30-50 micras)</option>
    <option value="15">Laminado brillo (30-50 micras)</option>
    <option value="14">Plastificado brillo (125 micras)</option>
    <option value="13">Plastificado brillo (75 - 80 micras)</option>
    <option value="11">Adhesivado</option>
    <option value="7">Kappa-fix 10 mm (cartón pluma alta densidad)</option>
    <option value="6">Kappa-fix 5 mm (cartón pluma alta densidad)</option>
    <option value="5">Laminado Mate (proteccion uv)</option>
    <option value="4">Laminado Brillo (proteccion uv)</option>
    <option value="3">Aluminio</option>
    <option value="2">Bastidor 4 cm</option>
    <option value="1">Bastidor 2 cm</option>
    <option value="9">Dibond 3mm (sandwich aluminio + pvc)</option>
</select>

The first option is absolute: If you select it, it clears all other options.

<option selected="" value="-1">Sin Acabados</option>

But If I have this option selected, I want delete it, and leave the new selected option when I select another option:

<option selected="" value="-1">Sin Acabados</option> <!-- Selected yet -->
<option value="20">Kappa-fix 10mm (PVC)</option> <!-- I want select this, clearing value =-1 from the selected options. -->

My validation function:

EDIT2:

function validar_enmarcado(){
    var obj = document.getElementById ("Enmarcado");
    var enmarcado = new Array();

    var i = 0;
    var u = 0;
    var y = 0;
    var cont = 0;
    var opt1 = "";
    var opt2 = "";

    enmarcado = obj.selectedOptions;
    if(enmarcado.length>1){
        for (i=0; opt1=enmarcado[i];i++){
            for (u=i+1; opt2=enmarcado[u];u++){
                if(opt1.value == -1){
                    if(opt2.value >= 1){
                        $("#Enmarcado").chosen();
                        $("#Enmarcado").val(-1).trigger("chosen:updated");
                        jAlert("Si elige Sin Acabados, no puede haber acabados en la lista.","Error de Acabado");
                    }
                }
                if(opt1.value == 6 || opt1.value == 7 || opt1.value == 9 || opt1.value == 19 || opt1.value == 20){
                    if(opt2.value == 6 || opt2.value == 7 || opt2.value == 9 || opt2.value == 19 || opt2.value == 20){
                        jAlert("No puede elegir dos Soportes al mismo tiempo, elimino uno.","Error de Acabado");
                    }
                }
                if(opt1.value == 1 || opt1.value == 2){
                    if(opt2.value == 1 || opt2.value == 2){
                        jAlert("No puede elegir dos Bastidores al mismo tiempo, elimino uno.","Error de Acabado");
                    }
                }
                if(opt1.value == 4 || opt1.value == 5 || opt1.value == 13 || opt1.value == 14 || opt1.value == 15 || opt1.value == 16 || opt1.value == 17 || opt1.value == 18){
                    if(opt2.value == 4 || opt2.value == 5 || opt2.value == 13 || opt2.value == 14 || opt2.value == 15 || opt2.value == 16 || opt2.value == 17 || opt2.value == 18){
                        jAlert("No puede elegir dos Laminados o Plastificados al mismo tiempo, elimino uno.","Error de Acabado");
                    }
                }
            }
        }
    }
}

The problem is:

<option selected="" value="-1">Sin Acabados</option>

This is the default option, and I want to remove it when I select other option. But I dont know how to do it, without removing the other validations.

I cannot get the last option selected from the list of $("#Enmarcado").selectedOptions[] because a multiple select always reorder the selected items with the $("#Enmarcado").options[] order.

Thanks for your help!

EDIT3:

I just found a bit of code that give me the "selected option value":

$("#Enmarcado").chosen().change(function() {
    alert(+$(this).val());
});

But it sends me NaN when I select multiple options. Could anyone help to insert this in my validation function?

Response to this: delete the + before $(this).val() and it will send you an string with the selected values. But is not that I want: I need know WHICH option I just selected(or clicked).

2 Answers2

1

I found the solution by using Google today:

var foo = [];
$('#EnmarcadoChosen :selected').each(function(i, selected){
    foo[i] = $(selected).text(); // you can get the .value() too, and compare it.
});
honk
  • 9,137
  • 11
  • 75
  • 83
0

It could be as simple as: $("#Enmarcado option[value='-1']").remove();

But if the above doesn't work in your context, could you build on this answer by VirtualTroll? (If so, upvote his, too.) In your case, that could look like:

for (i=0; i<obj.length; i++) {
   if (obj.options[i].value=='-1') {
     obj.remove(i);
   }
}

And note a few gentler approaches, that might make it easier to turn the default back on, later:
1: $("#Enmarcado option[value='-1']").hide();
2: $("#Enmarcado option[value='-1']").attr('disabled','disabled');


UPDATE:

From your new comments below, it's now clear that you're not looking to "delete" or "remove" the option -- you're only looking to clear, or "unselect", the selection of the option. Though I did not know an answer to that, I do think I found answers to that. (Again, if this information works for you, please upvote their answers too.)

(1) Note Sudhir Kesharwani's solution here. I think his code clears all selections... so, attempting a variation of his code, would this shorthand version work?:

$("#Enmarcado option[value='-1']").removeAttr("selected");
$("#Enmarcado").trigger("chosen:updated");

And (2), you might find the documentation useful. In Joseph Silber's answer here, he refers to the documentation here, saying "(find the section titled Updating Chosen Dynamically)." It explains that you have to trigger chosen:updated after any selection changes.

I hope that helps...

Community
  • 1
  • 1
Doug_Ivison
  • 778
  • 7
  • 17
  • In my function validar_enmarcado() I can remove all options when I select the 1st option with: `$("#Enmarcado").chosen(); //resets the select $("#Enmarcado").val(-1).trigger("chosen:updated"); //selects the 1st option` but when I have the first option selected YET, I want to delete it when I select another option. BUT I don't want to delete the options from obj.option[], I only want delete them from obj.selectedOptions[]; My problem aren't the commands, is the logarythm: I can delete the options with a loop and `obj.selectedOptions[i].selected=false;` – Barragán Louisenbairn Feb 27 '14 at 16:51
  • @BarragánLouisenbairn So, is it that you want to **"unselect"** that value (the default), when other options are selected? – Doug_Ivison Feb 27 '14 at 18:57
  • yes! that's it! But i don't want lose the other validation: 1.- When I click in Default value, all other options are "unselected". – Barragán Louisenbairn Feb 28 '14 at 10:45
  • @BarragánLouisenbairn Did the above help? If there are still issues, a you post them (replying here or appending a question edit), I'll be happy to see if I can help. And... if I don't have answers, I'd be willing to put a small bounty on your question... if you still need that. – Doug_Ivison Mar 06 '14 at 14:51
  • 1
    1.- I edited the question a few days ago, but no one answer how to solve my problem correctly: how to know which element I just selected(clicked) in a select multiple. 2.- My solution: changing how my code works, avoiding the need of know which element the user just click in each iteraction. Or in other words: delete & bypass it. I won't tell how I bypass that need, is part of my algorythm. sorry, but cannot post more of the inner code of my app. – Barragán Louisenbairn Mar 07 '14 at 18:54
  • I understand not wanting to reveal too much, publicly. Note: my answer edit, above, which gave a lot of new information, was *AFTER* your last question edit. Did you try that? – Doug_Ivison Mar 07 '14 at 20:55
  • mmm yes, you can use that code to delete the option, but... how can I know the difference between the case when I select the default option before I selected another option, and the case when I had the default option seleted yet, and I select another option? because the "selectedOptions" array is being reorderer always the options... (sorry if I don't know how to explain this very well) – Barragán Louisenbairn Mar 12 '14 at 19:10
  • ` for (i=0; opt1=enmarcado[i];i++){ for (u=i+1; opt2=enmarcado[u];u++){ if(opt1.value == -1){ $("#Enmarcado").chosen(); $("#Enmarcado").val(-1).trigger("chosen:updated"); jAlert("Si elige Sin Acabados, no puede haber otros acabados en la lista. Quite esta opción si quiere hacer cambios.","Error de Acabado"); return false; } else{ $("#Enmarcado option[value='-1']").removeAttr("selected"); $("#Enmarcado").trigger("chosen:updated"); }` – Barragán Louisenbairn Mar 12 '14 at 19:12