onChange event on the fly using either JQuery or vanilla JS

Advertisements

I have an input text that disable a button if it’s empty and enable it when this change.

Usually I would use a JQuery onchange event to enable or disable the button once the input value change but they asked me to do it on the fly. (disable or enable the button while I am typing), not after I finished writing inside my input.

I can only use either JQuery or vanilla JS

So: is there any option for onchange event that works on the fly?

PD: I know that as a requirement is a bit silly and not quite useful, but this should be doable.

>Solution :

I would use input (handles typing AND paste)

document.getElementById("inp").addEventListener("input",(e) => {
  document.getElementById("btn").disabled = e.target.value.trim() === "";
})
<input id="inp" /> <button id="btn" disabled>Click</button>

Leave a ReplyCancel reply