Grey out submit button on html form until text is inserted

From The IT Community
Jump to: navigation, search

How to grey out the submit button on a HTML form until a text is inserted:

Insert the following into the <head>-section of your page:

<script type = "text/javascript">
function check() {
var filled = 0
var x = document.form1.title.value;
x = x.replace(/^\s+/,""); // strip leading spaces
if (x.length > 0) {filled ++}
if (filled == 1) {
document.getElementById("submitForm").disabled = false;
document.getElementById("submitForm").disabled = true

and the following into your <html>-section:

<form name="form1" action="" method="get" />
<input type="text" id="title" name="search" value="" onkeyup = "check()" />
<input type="submit" value="submit" id="submitForm" disabled="disabled" />

and you are done, as soon as one character (other than a space) is entered the text field, the submit button becomes active.

Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.

Translate this page:

Articles found in the same category:
(max. 20 shown)