Validate Onclick event in JavaScript

2

I want to perform a validation on a button to clean my forms by clicking on the exit button:

if (document.getElementById('btnsalirmodalf').onclick == true) {
    document.getElementById('asignadoa').value == '';
    document.getElementById('Estatus').value == '';
    document.getElementById('porcentaje').value == '';
    document.getElementById('exampleFormControlTextarea1').value == '';
    document.getElementById('exampleFormControlTextarea2').value == '';
}

The problem is that I do not know if in this part:

document.getElementById('btnsalirmodalf').onclick == true

I'm doing it well since I do not make any changes to click on exit. Do you have a suggestion please?

    
asked by Chemox 06.12.2018 в 19:40
source

2 answers

1

Create a function that does what you need and at the time of waxing (to put the input blank), assigns the value of "" only with a "=" not with two.

Try this code:

function limpia() {
  document.getElementById("asignadoa").value = "";
  document.getElementById("Estatus").value = "";
}
<button type="button" id="btnsalirmodalf" onclick="limpia()">Limpiar</button>
<input type="text" id="asignadoa"/>
<input type="text" id="Estatus" />
...
    
answered by 06.12.2018 / 20:00
source
0

You have to define a function by clicking on the button.

document.getElementById('btnsalirmodalf').onclick = () => {
  // Código a ejecutar al hacer click
}

Within the click function you have to define the elements to which you are going to change the value. That's why your document.getElementById works for you. But in your example you are using == instead of = .

document.getElementById('btnsalirmodalf').onclick = () => {
  document.getElementById('asignadoa').value = '';
  document.getElementById('Estatus').value = '';
  document.getElementById('porcentaje').value = '';
  document.getElementById('exampleFormControlTextarea1').value = '';
  document.getElementById('exampleFormControlTextarea2').value = '';
}
    
answered by 06.12.2018 в 19:46