I have the following form:
If you are a beginner of javascript, a better option would be
Add HTML as follows
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Now add validateForm()
as
function validateForm() {
// Validate all fields in the form
return true; // if all form entries are valid. else return false
}
Note: This will not grey-out the submit button. But in this case submit will work only if all the entries are valid.
Jquery would do that. On the .keyup event have jquery check if both field's lengths are > 0 and change the button to be disabled or not.
$('#yourButton').button("disable");
$('.fields').bind('keyup', function() {
var nameLength = $("#sub_first_name").length;
var emailLength = $("#sub_email").length;
if (nameLength > 0 && emailLength > 0)
{
$('#yourButton').button("enable");
}
} );
The script would go in the <head></head>
of your document. Like so.
<script type="text/javascript" src="yourpath"></script>
One way would be to match the values of the inputs and then remove the grayed out class from your button if the values aren't still the same as the default. Here is a demo.
HTML
<input type="text" value="some text" class="text-input">
<input attr="" type="submit" value="submit" class="submit">
CSS
.disabled {opacity:.2;}
jQuery
if ($('.text-input').val() == "some text" ) {
$('.submit').addClass('disabled');
}
http://jsfiddle.net/APGmy/11/