Skip to content
Advertisement

Disable button whenever a text field is empty dynamically

Here’s my code:

JavaScript

This works but still not efficient since the user can delete the text inside the text box and click the button while he’s holding on DELETE key. Is there a more efficient way to achieve this using javascript?

Advertisement

Answer

Add a check when the button is clicked to see if there is any text. If there isn’t, pop up an alert box (or some other form of feedback) to tell the user to enter data, and don’t do the button functionality.

Example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement