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
}
};
JavaScript Checkbox 1 Checking if a checkbox is checked. First, select the checkbox using the selecting DOM methods such as getElementById () or querySelector (). 2 Getting values of multiple selected checkboxes. To accomplish this, you need to add two more HTML attributes to each checkbox: name and value . 3 Check / Uncheck all checkboxes. ...
To select the selected checkboxes, you use the querySelector () method: const checkboxes = document .querySelectorAll ( 'input [name="color"]:checked' ); Code language: JavaScript (javascript) And gather the value of each checkbox: let colors = []; checkboxes.forEach ( (checkbox) => { colors.push (checkbox.value); });
and you want to add an EventListener to this checkbox using javascript, in your associated js file, you can do as follows: checkbox = document.getElementById ('conducted'); checkbox.addEventListener ('change', e => { if (e.target.checked) { //do something } });
To get the state of a checkbox, whether checked or unchecked, you follow these steps: First, select the checkbox using the selecting DOM methods such as getElementById () or querySelector (). Then, access the checked property of the checkbox element.
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"); } }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With