Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

onChange event on the fly using either JQuery or vanilla JS

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading