Enable submit button when checked

 

Disable or Enable submit button when checkbox checked

  

coding-zon-enable-disable-formbutton

Today we are going to learn How to disable and enable the submit button depending on checkbox status.

To disable and enable buttons, use the prop() method in jQuery.

Submit button is disabled first, when the user  checks  it if he agrees with the condition.  Then the Submit button is enabled.

The jQuery code snippet below uses the class name 'agree'. It uses prop to set the attribute.

Code Example for enable disable submit button :


 index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
 
<title>e-Trainings Demo on 
 Disable or Enable a submit button checkbox checked jQuery</title>


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<script>

$(document).ready(function(){
 
$('form input[type="submit"]').prop("disabled", true); 
 
$(".agree").click(function(){
 
if($(this).prop("checked") == true){
 
    $('form input[type="submit"]').prop("disabled", false);
}
else if($(this).prop("checked") == false){
 
    $('form input[type="submit"]').prop("disabled", true);
} 
});
});

</script>
</head>
<body>
<div class="cotainer">
<div class="row">
<div class="col-md-4">
<form>
	<div class="form-group">
		<label>Name: <input type="text" class="form-control" name="username"></label>
	</div>     
	<div class="form-group">
		<label>Email: <input type="email" class="form-control" name="email"></label>
	</div>      
	<div class="form-group"> 
		<label>
		<input type="checkbox" class="agree"> I agree to terms and conditions.</label>
	</label>
</div> 
<div class="form-group">
	<label><input type="submit" class="btn btn-primary" value="Submit"> </label>
</div>
</form>
</div>
</div>
</div>
</body>
</html>  

 

Result:


 

e-trainings-formbuttondisble-enable

 


I hope this tutorial helps  you  in Disabling the Submit Button Until a Checkbox Is Checked. Please comment below if you have any suggestions.

 

Comments

Popular posts from this blog

Recently viewed list in PHP Mysql

Import CSV File Into MySQL Using PHP

Shoppingcart using PHP Sessions - Miniproject