2

I would like the "Send" button to be disabled if the input is empty. I want to manage this in the JavaScript file. I know that it is possible in HTML to call a function on the input but I prefer to use an event in the js file.

https://codepen.io/leakcim-web/pen/gOYPpqo

//javascript
let inputElt = document.getElementById('input');
let btn = document.getElementById('button');

if (inputElt.value !== '') {
  btn.disabled = false;
} else {
  btn.disabled = true;
}
<input placeholder="Enter some text" name="name" id='input' />
<button id='button'>Réserver</button>

4
  • 1
    use input event Commented Aug 13, 2019 at 12:14
  • use addEventListener in your js to your inputElt, and update the btn disabled from the listener... Commented Aug 13, 2019 at 12:17
  • I had a similar problem and this question / answer helped me a lot. Thank you. Commented Dec 30, 2020 at 4:05
  • I posted my probelm and solution here if it helps anyone stackoverflow.com/questions/65501771/… Commented Dec 30, 2020 at 4:05

2 Answers 2

7

You can use addEventListener to add an event to the textbox and enable/disable as appropriate

let inputElt = document.getElementById('input');
let btn = document.getElementById('button');

inputElt.addEventListener("input", function(){
  btn.disabled = (this.value === '');
})
<input placeholder="Enter some text" name="name" id='input'/>
<button id='button' disabled>Réserver</button>

Sign up to request clarification or add additional context in comments.

6 Comments

I would go with btn.disabled = !this.value; and by default set disabled property on the DOM instead of setting it in JS otherwise 👍
@AdrienMartinet I agree on setting disabled in the DOM. Updated. The logical check I chose to keep close to the OP, but your way would also work just fine.
It's worth including a call to String.prototype.trim(), otherwise a string of white-space can be submitted (I realise that this isn't a stated requirement though).
Thank you for your help. @Jamiec I do not understand this part: btn.disabled = (this.value === ''); Disabled is not boolean? I do not understand why it is assigned the value this.value === ''
Yes Disable is a boolean attribute w3schools.com/tags/att_disabled.asp :)
|
2

Just as amusement, you can do this in just CSS and html, reference Matching an empty input box using CSS

#input:invalid + button {
    opacity: 0.5;
    pointer-events: none;
}
<input placeholder="Enter some text" name="name" id='input' required="required"/>
<button id='button'>Réserver</button>

And if you have something in between the button and input you can use #input ~ #button. also you don't need the id attributes you can use type="submit" on button then use input ~ [type="submit"] then it will work with any input at the same nesting.

1 Comment

Interesting alternative :) Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.