E
E
Elena2020-05-09 18:33:08
JavaScript
Elena, 2020-05-09 18:33:08

How to make a non-clickable button when there is no text in the field and active when text appears in the input field?

Hello everyone) The question is probably simple to the point of horror, but I did not find a clear answer on the Internet. I have a form. It has an input for entering data and a button for saving. How to make the button inactive when there is no text in the field and when there is text it is active? Or direct where to read about it)
Link to the code https://codepen.io/Ponomareva1994/pen/NWGMxEx

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tigran Abrahamyan, 2020-05-09
@Elena0394

const input = document.querySelector("input");
const button = document.querySelector("button");

button.disabled = true;

input.addEventListener("input", function() {
  button.disabled = !this.value.length;
})

R
Rsa97, 2020-05-09
@Rsa97

Attach a handler to the 'input' event of the input, check the content in it and activate/deactivate the button.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question