jQuery: Disable/enable the form submit button
jQuery Practical exercise Part - I : Exercise-4
Disable/enable the form submit button.
Disable the submit button until the visitor has clicked a check box.
Sample Data :
<body> <input id="accept" name="accept" type="checkbox" value="y"/>I accept<br>
<input id="submitbtn" disabled="disabled" name="Submit" type="submit" value="Submit" />
</body>
Sample solution :
HTML Code:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>Disable/enable the form submit button</title>
</head>
<body>
<input id="accept" name="accept" type="checkbox" value="y"/>I accept<br>
<input id="submitbtn" disabled="disabled" name="Submit" type="submit" value="Submit" />
</body>
</html>
JavaScript Code :
$('#accept').click(function() {
if ($('#submitbtn').is(':disabled')) {
$('#submitbtn').removeAttr('disabled');
} else {
$('#submitbtn').attr('disabled', 'disabled');
}
});
See the Pen jquery-practical-exercise-4 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: Disable right click menu in html page using jquery.
Next: Fix broken images automatically.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics