How to reset select using Javascript?

Asked

Viewed 87 times

0

watch the image! inserir a descrição da imagem aqui

I’m trying to clear a modal form, I can already clear the other fields with javascript, but I did not have the same result with the list, I tried to do it by the back-end, but I did not have success, so I would like to see the possibility of trying reset the list, would anyone have an idea how to do that? That’s the code block in question! My Javascript file

function justificarRemessa() {

    $('#idPeriodoInicio').val("");
    $('#idMotivo').val("");
    $('#idPeriodoFim').val("");
    $('#idUJ').val("");

    document.getElementById('idUJ').selectedIndex = "0";
    document.getElementById('idUJ').options[0].text = " - ";




    //  var form = document.getElementById('formCadastrarJustificativa');

//  for( var i = 0; i < form.elements.length; i++) {
//      console.log(form.elements[i].type);
//      if (form.elements[i].tagName === 'SELECT') {
//          form.elements[i].selectedIndex = '0';
//      }
//  }


    $('#modalJustificar').modal('show');

}
  • 1

    Cannot change the value of select? document.getElementById("omeuselect").value = "-"; Where is your code?

  • If select is inside a form just give one form.reset()

  • Look at this answer used in another question, maybe I can help. https://stackoverflow.com/questions/44266977/how-to-set-select-box-to-default-value-on-modal-close

  • tried this way did not work, a form, is not a common form, is a modal :(

  • @Sumback I will try this solution!

  • Try calling the modal close function and in it vc set the field value to null.

  • @fernandosavio how am I going to use the form.reset() ?

  • @Sumback didn’t work

  • @fernandosavio gave this message Uncaught Typeerror: $(...)[0]. reset is not a Function

  • Just you select your <form> with document.getElementById() or something similar and then use the method reset() form so that each input returns to its initial state. Look a very simple example

Show 5 more comments

1 answer

1


Use this function for when the modal is closed

$('#myModal').on('hidden.bs.modal', function (e) {
  form.reset()
});

Or

$('#myModal').on('hidden.bs.modal', function (e) {
   justificarRemessa()
});
  • 1

    Solved, thanks, thank you very much :)

Browser other questions tagged

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