I created a form where users can input words in a textarea as tags and submit them as a string using JavaScript. The feature I want to add is to disable the submit button whenever the textarea is empty (does not contain any tags).
Here is what I have tried so far:
HTML
<form>
<textarea onkeyup="success()" name="tags" id="tag-input1" required>
</textarea>
<p class="instruction">Press enter to add a new word</p>
<!-- Disable Submit Button -->
<input type="submit" id="submit" class="save" value="Submit">
</form>
JavaScript
function success() {
if (document.getElementById("tag-input1").value === "") {
document.getElementById('submit').disabled = true;
} else {
document.getElementById('submit').disabled = false;
}
}
2
Answers
I think you could check value length.
At first, try to add disabled attribute to your submit button in html.
Then in here is your success function code;
As per my knowledge, that is not how disable works. Disable is a html attribute that stops user from writing data in the perticular text.
Try providing error message when value of perticular message is empty
You can use conditional statements for that