Found lots of answers for this in jQuery, but I want to do it in pure Javascript. This code doesn't work. It enabled when all the fields are filled, but enables again when I remove the text in one of them. What is wrong with this?
<form action='' method='post'> <input type='text' class='req' id='required_0' onkeydown='enableSubmit()' required> <input type='text' class='req' id='required_1' onkeydown='enableSubmit()' required> <input type='text' class='req' id='required_2' onkeydown='enableSubmit()' required> <input type='submit' value='Submit' disabled> </form> <script> function enableSubmit(){ let inputs = document.getElementsByClassName('req'); let btn = document.querySelector('input[type="submit"]'); let disabled = false; for (var i = 0; i < inputs.length; i++){ let changedInput = document.querySelector('#required_' + i); if (changedInput.value.trim() == "" || changedInput.value == null){ btn.disabled = true; console.log("Button disabled"); } else { btn.disabled = false; console.log("Button enabled") } } } </script> EDIT: Thank you for all the answers, I didn't think I would get that many - so I don't really know who I should choose as "answer" for the cred. Please comment below with your pick (and why) to help me choose.