You are using as a function name a name of a native Javascript method. This is generating a type of conflict. Clicking the button will call the native function reset()
JS reset form and not called function you gave the same name of reset
.
Try to name functions and variables where you can be sure that
are not names of native functions or methods or reserved words of the
language. As the language is in the English language, a good output to avoid
conflicts is to give names in Portuguese.
What you have to do is give a different name to the function, it can be Reset
(uppercase first letter, because JS differentiates uppercase from lowercase) or any other name you like (not reserved by language). In the example below I switched to resetar
.
In addition, since you assigned the form to the variable form
, doesn’t need document
, otherwise will give error. Then you should remove these document
.
It may also simplify the if
only with if(set)
instead of if(set == true)
. The variable alone in if
already indicates that it should be true
(just as !set
indicates that it must be false
):
function resetar() {
var form = document.getElementById("form");
var nome = form.nome.value;
var set = confirm("Deseja apagar os dados do formulário?");
if (set) {
alert('Os campos do formulário foram resetados!');
form.reset();
form.nome.focus();
}
}
<form id="form">
<input type="text" name="nome">
<button type="button" class="btn btn-dark btn-lg" onclick="resetar();">Limpar</button>
</form>
<button type="button" class="btn-dark btn-lg" onclick="reset();">Clear</button>
– Matheus Mendonca
What "rest of the functions" is talking about ?
– Isac