Button type reset calls a function

Asked

Viewed 515 times

1

I have a web form on which I put a input type="reset", to delete the form data in case the user needs it. The button works and is erasing the data as desired, but I’m having a problem.

I have a select, where depending on the value I select on it, it changes the content, and the select is inside the form. select comes with a default value where no contents appear.

When I click the reset button, it returns the select the default value and the content does not disappear as expected. I will leave an example:

function mostrarConteudo(){
  if(document.getElementById("sel").value == "Nao"){
    document.getElementById("div").style.display = "none";
  }else{
    document.getElementById("div").style.display = "";
  }
}

window.onload = function () {
  mostrarConteudo();
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<form>
  <select id="sel" class="form-control w-100" onchange="mostrarConteudo()">
    <option value"">Nao</option>
    <option value"S">Sim</option>
  </select>
  
  <br>
  
  <div id="div" style="background-color: red; width: 500px;" class="ml-4">Valor da div</div>
  
  <br>
  <br>
  
  <button type="reset">Resetar Form</button>
</form>

Note that when leaving select with the option "yes" and reset, it changes the value to "no", but the div does not disappear.

I tried to put in select one onreset to call the function again but it did not work.

I’ve tried putting a onclick on the button, also did not work.

What can I do to correct this mistake?

EDIT:

Error when I try to pass a parameter, as suggested in Sam’s answer: (in this case, my function is called openOptions())

inserir a descrição da imagem aqui

  • Check out this link: https://stackoverflow.com/questions/30214564/html-form-reset-not-doing-triggering-select-onchange

3 answers

1


Places an attribute onclick calling the function and passing any value:

<button type="reset" onclick="mostrarConteudo(this)">Resetar Form</button>

If the value of the parameter passed exists in the function, it means that the reset button has been clicked:

function mostrarConteudo(e){
                    //   ↑----------------------------↓
  if(document.getElementById("sel").value == "Nao" || e){
    document.getElementById("div").style.display = "none";
  }else{
    document.getElementById("div").style.display = "";
  }
}

window.onload = function () {
  mostrarConteudo();
}
<form>
  <select id="sel" class="form-control w-100" onchange="mostrarConteudo()">
    <option value"">Nao</option>
    <option value"S">Sim</option>
  </select>
  
  <br>
  
  <div id="div" style="background-color: red; width: 500px;" class="ml-4">Valor da div</div>
  
  <br>
  <br>
  
  <button type="reset" onclick="mostrarConteudo(this)">Resetar Form</button>
</form>


You can also hide the direct element in the onclick, without going through the job:

<button type="reset" onclick="$('#div').hide()">Resetar Form</button>

Another way is to create one Event Listener pro button and call the function after a small delay using setTimeout():

function mostrarConteudo(){
  if(document.getElementById("sel").value == "Nao"){
    document.getElementById("div").style.display = "none";
  }else{
    document.getElementById("div").style.display = "";
  }
}

$("[type=reset]").on("click", function(){
   setTimeout(mostrarConteudo, 1);
});

window.onload = function () {
  mostrarConteudo();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <select id="sel" class="form-control w-100" onchange="mostrarConteudo()">
    <option value"">Nao</option>
    <option value"S">Sim</option>
  </select>
  
  <br>
  
  <div id="div" style="background-color: red; width: 500px;" class="ml-4">Valor da div</div>
  
  <br>
  <br>
  
  <button type="reset">Resetar Form</button>
</form>

  • For my code, this would not be possible, because it has places in the js itself where I need to call the function (as in window.onload), but in my code is giving error when I call, for not passing any parameter.

  • If you do not pass any parameters, the e is Undefined and no mistake. But I added another solution to the answer.

  • as stated in the comment on the reply to Hugo, the ideal is that I call the function, as in his first example.

  • I’ll leave a print on the question with the error

  • I added another way at the end of the answer. There are 1 million ways to do this

  • opa, now yes. Your last answer worked perfectly.

Show 1 more comment

0

Good afternoon,

Change the HTML select so that the values are for example 1 and 2 and the type reset button puts type button and with an onclick to call the resetForm function()

Options within the select:

<option value"1">Nao</option>
<option value"2">Sim</option>

Reset button:

<button type="button" onclick="resetForm()">Resetar Form</button>

Then on the JS in the decision structure of the function show:

if(document.getElementById("sel").value == 1){
    document.getElementById("div").style.display = "none";
}else{
    document.getElementById("div").style.display = "";
}

This is a new function to reset the folder:

function resetForm(){
    document.getElementById('sel').value = 1;
    document.getElementById("div").style.display = "none";
}

This should solve your problem

-1

It is easy to... js does not call the function "showConducer" again because it is only called each time it Reload on the page. That is the problem is that the function is not called after.

  • and how I call it after resetting ?

  • <option value"">No</option> this is wrong! Check this ... value='not'

Browser other questions tagged

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