Reputation: 413
I have code like this at the bottom of my form.
<p>
<input type="checkbox" id="agreed">
I agree to keep my Username and Password confidential and uphold
the integrity of this pharmacy
</p>
<input type="submit" id="submit" disabled class="formbutton button-primary" value="Go">
I want this a listener in JavaScript to be able to enable the submit button. I know it might be wrong or something but my JavaScript looks sort of like this
function track() {
if ( document.getElementById("agreed").checked==true ) {
document.getElementById("submit").removeAttribute("disabled");
} else {
document.getElementById("agreed").checked==false
}
};
Upvotes: 31
Views: 113660
Reputation: 966
You can do this for the input:
<input type='checkbox' onchange='handleChange(this);'>Checkbox
And this for enabling the button:
function handleChange(checkbox) {
if(checkbox.checked == true){
document.getElementById("submit").removeAttribute("disabled");
}else{
document.getElementById("submit").setAttribute("disabled", "disabled");
}
}
Upvotes: 60
Reputation: 1714
Try Below for your requirement using jQuery.
$('#checky').click(function(){
if($(this).attr('checked') == false){
$('#postme').attr("disabled","disabled");
}
else {
$('#postme').removeAttr('disabled');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<input type="checkbox" checked="checked" id="checky"><a href="#">I agree to keep my Username and Password confidential and uphold
the integrity of this pharmacy</a>
<br>
<input type="submit" id="postme" value="submit">
Using JavaScript, Please refer This
Upvotes: 3