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()
)
Check out this link: https://stackoverflow.com/questions/30214564/html-form-reset-not-doing-triggering-select-onchange
– Maycon F. Castro