I have been trying to make a simple app with 3 fields like this:
<div id = "nameInput" >Name:</div><input id = "nameInputField" type = "text" name = "userName" />
<div id = "emailInput" >Email:</div><input id = "emailInputField" type = "text" name = "email" />
<input id = "termsCheck" type = "checkbox" name = "terms" />
The problem I am having is that I keep needing to try to wrap it in a form to get the checkbox to register as checked when it is. I DO NOT want to use a form because I don't ever want to submit anything.
Here is the JS for the checkbox as well. It is always marked as unchecked:
if ($('#terms').checked == true) {
//Rest of code
Is there a way to make this without using a form or is there a reason that my checkbox is never registered as checked?
Contrary to popular belief, having a checkbox on every form that collects is not required under the GDPR. A checkbox isn't required if the user, by submitting a form for its stated use, gives explicit consent.
The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!
var text = $('input[type="checkbox"]'). text(); alert(text);
<input id = "termsCheck" type="checkbox" name="terms" />
JS:
pre jQuery 1.6:
if($('#termsCheck').is(':checked')){}
after jQuery 1.6:
if($('#termsCheck').prop('checked')){}
Two issues.
First, you're referencing the name of the input instead of its ID.
Second, in order to use the checked
property, it must be done on the DOM element, not the jQuery object.
if( $('#termsCheck')[0].checked ) {
// ...
}
This is accessing the DOM element at index 0
of the jQuery object, then accessing its checked
property.
You could also use the get()
[docs] method to retrieve the DOM element.
if( $('#termsCheck').get(0).checked ) {
// ...
}
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