Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a modal with this dropdownlist it is works fine, but when i close i would like to clear this dropdown and put the default value.
<div class="situacaoPedido col-md-12">
                            <select id="StatusPedidoSaveId" name="StatusPedidoSaveId" class="select2">
                                <option value="" selected>Selecione</option>
                                <option value="1">Pendente Compra</option>
                                <option value="2">Aguardando Pagamento</option>
                                <option value="3">Confirmado</option>
                                <option value="4">Aprovado</option>
                                <option value="5">Em Separação</option>
                                <option value="6">Enviado</option>
                            </select>


What I have tried:

i tried this javascript code:

$("#modal-trocar-status").on("show.bs.modal", function () {
    $("#DataPedido").val('');
    $("#numPedido").val('');
    $("#observacao").val('');
    //$('#StatusPedidoSaveId').prop('selectedIndex', 0);
    //document.getElementById('StatusPedidoSaveId').selectedIndex = 0;
    $("#StatusPedidoSaveId").val($("#StatusPedidoSaveId").data("default-value"));
    $('.obsAlteracao').hide();
    $('.dadosAlterados').hide();
});


this work in datapicker, textbox but dos not work to a select dropdown element
Posted
Updated 8-Nov-20 23:59pm
v2
Comments
[no name] 7-Nov-20 20:50pm    
I don't think everyone knows what "reset this dropdown" means. You should describe what you're "seeing" and what you want to see / happen.

Based on your CSS class name, I'm guessing you're using Select2[^]?

If that's the case, you'll need to trigger the change event after setting the value, as shown in the documentation[^]:
JavaScript
$("#StatusPedidoSaveId").val('').trigger('change');
 
Share this answer
 
Comments
Fernando_Costa 9-Nov-20 9:50am    
Thanks a lot.
You can make use of the defaultSelected property of an option element[^]:

On close of your modal, have the following code:
JavaScript
$('#my_select option').each(function () {
    if (this.defaultSelected) {
        this.selected = true;
        return false;
    }
})

Make sure to have the default option have property selected like:
HTML
<option value="b" selected="selected">b</option>
 
Share this answer
 
Comments
Fernando_Costa 8-Nov-20 16:55pm    
I tried but it didn't work either, in dropdonw, the last choice appears and not the text "Selecionar"
Sandeep Mewara 9-Nov-20 0:38am    
Would help if you debug and see the execution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900