SasNarSak
SasNarSak

Reputation: 95

How to apply a function when clicking on any <input>?

I need to call the pocniKviz function when I click on any input element.

function pocniKviz() {
    vreme1 = new Date();
    var vremeOd = vreme1.getHours() + ":" + vreme1.getMinutes() + ":" + vreme1.getSeconds();
    document.getElementById("vremeOd").innerHTML = vremeOd;
    // kada se zapocne kviz iskljucuje se dugme za pocetak kviza i ukljucuje dugme za zavrsetak kviza
    document.getElementById("btnZapocniKviz").disabled = true;
    document.getElementById("btnZavrsiKviz").disabled = false;
}

Like these

<input type="radio" value="Da" name="pitanje1">Da</input>
<input type="checkbox" value= "HTTP" name="pitanje2">HTTP</input>
<input id="pitanje3">

Upvotes: 0

Views: 34

Answers (1)

marzelin
marzelin

Reputation: 11610

Add one listener to document.body and conditionally run the function if the target is input:

document.body.addEventListener("click", (e) => {
  if (e.target instanceof HTMLInputElement) {
    pocniKviz();
  }
})

Upvotes: 1

Related Questions