How do I get the result of a checked checkbox? - html

I'm really new to HTML, but I can't find anywhere how to return the variable from a check box. I've figured out how to get variables from forms and inputs, but I don't know how to get them from check boxes. Basically, I want a check box that if checked, will return the value yes. If not checked, it doesn't have to return a value, but no would be nice.

When submitting a form, if a checkbox is checked, it will be included in the values collection submitted to the server. If not checked, it will be omitted (as if that checkbox did not exist).
In JavaScript, you can find out whether the checkbox is checked by the checked property:
//returns true or false
var isChecked = document.getElementById('id_of_checkbox').checked;
In ASP.NET, the CheckBox control has a boolean property Checked.

Assign something to the value attribute. You should get the value of the checked box, if there is one in the form values on postback. Note that I've made it a radio input in the case where you want yes or no. For a radio you need to assign the same name, but different ids (if you assign an id at all). You'd want to use checkboxes in the case where you can have more than one value selected or only care to get the value when selected. These types of checkboxes should have different names.
<input type="radio" id="radio_yes" name="radio_btn" value="Yes" /> Yes
<input type="radio" id="radio_no" name="radio_btn" value="No"
checked="checked" /> No
If you only want a single check box with the value yes to be returned if checked, then you can use a check box. You won't get any value if it isn't checked.
<input type="checkbox" id="chk_box" name="chk_box"
value="Yes" /> Do you want this?
On the server-side, you look for the value corresponding to the "radio_btn" (first example) or "chk_box" (second example) key in your form parameters.

Use the 'checked' property.
var checkbox = document.getElementById('checkboxId');
var checked = checkbox.checked;

look at the value of YourCheckboxName.Checked

If a checkbox is disabled, it will be omitted in the values collection submitted to the server even if it is checked:
<input type="checkbox" name="checkbox1">
<input type="checkbox" name="checkbox2" checked>
<input type="checkbox" name="checkbox3" disabled>
<input type="checkbox" name="checkbox4" disabled checked>
Looks like:
result of HTML sample code
Only checkbox2 is in the values collection submitted to the server, if the user submits the form without any changes.

Related

Set value of radio button input using URL Parameter only (WITHOUT JQuery or JavaScript)

I have an HTML form on a web page. I want to send users an email with a URL that they can click to fill out the form. I want to pre-populate the value of a radio button group using URL parameters only.
The platform I am using does not allow me to do any scripting of any kind. I need to do this using only the URL parameters.
This is trivial for other types of input tags. For example, if I have a page called form.html and within that page I have an input tag as follows:
<input name="firstname" type="text">
Then I can use the following URL to pre-populate the field with the value "James":
http://form.html?firstname=James
What I am looking for is how to do this with a radio button. For example, let's say my page form.html has a radio button group with three options as follows:
<input name="status" type="radio" value="New">
<input name="status" type="radio" value="Expired">
<input name="status" type="radio" value="Renewed">
How do I pre-set the value of this radio button group with a URL parameter?
I have tried the following:
http://form.html?status=Expired
But this doesn't work. Is there any way to do this without JS or JQuery? You may think I can just set the value I want to be selected by default using the checked=true attribute in the HTML itself, but the problem is that the value I want to pre-populate is different depending on the user, so I need to set it in the URL parameter.
Came across this when looking for same answer and surprised no one answered it. Bit late but I figured this out and thought I'd put in answer here. Basically, I ran an pageload function and grabbed the parameters via URLSearchParams then selected the radio button via an ID. For example:
Add IDs to your radio buttons:
<input id="new" name="status" type="radio" value="New">
<input id="expired" name="status" type="radio" value="Expired">
<input id="renewed" name="status" type="radio" value="Renewed">
Then run the javascript below upon pageload (add if statements to handle different statuses):
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const status = urlParams.get('status');
if (status == "expired") {
document.getElementById("expired").checked = true;
}
I based this off the following answer but modified it for radio butons: https://stackoverflow.com/a/70267794/7901491

need to require that at least one of 9 checkboxes be checked

I have a form that has a save button in it. I also have 10 checkboxes that I need to require the user to check at least one of them, then another set of 6 checkboxes that I need the user to check at least one of them. Is there a way to have a message pop up when the user tries to save the record without having made a selection in both of these areas?
Yes, you need a validated form with jQuery.
First thing is that your HTML will need a checkbox array like:
<input type="checkbox" name='cb[]' value='cb1'/>
<input type="checkbox" name='cb[]' value='cb2'/>
<input type="checkbox" name='cb[]' value='cb3'/>
<input type="checkbox" name='cb[]' value='cb4'/>
<input type="checkbox" name='cb[]' value='cb5'/>
if(jQuery('input[name=cb]:checked').length==0) {
console.log('Please check at least one checkbox');
}
Something like that...For your other checkbox group, use a different name like name="cb2[]" and jQuery would be:
if(jQuery('input[name=cb2]:checked').length==0) {
console.log('Please check at least one checkbox');
}
I hope this helps.

HTML invert checkboxes

I want to have a checked checkbox behave as an unchecked checkbox, and an unchecked checkbox behave as a checked checkbox. How can this be accomplished?
Context: In my template is a for loop that creates a checkbox for each filter condition. I want to show the user that the initial setting has all checkboxes checked, and they can uncheck them to exclude certain categories. The backend uses exclude statements, therefore it's necessary to invert the checkboxes.
Try the checked attribute
<input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
for more info w3school

input type = checkbox problem

Why is this line not working?
<input type="checkbox" checked="no"/>
<input type="checkbox" checked="false"/>
Even though i have specified no and false in checkbox value both are checked by default.
Thanks in advance :)
changing it to this will make the first unchecked, and the second checked :
<input type="checkbox" />
<input type="checkbox" checked />
To make the checkbox unchecked you need to remove the word "checked".
Try this -
<input type="checkbox" checked/>
The attribute "Checked" will keep your checkbox checked by default. If you do not have this attribute mentioned, then the checkbox will be unchecked by default.
<input type="checkbox" checked="checked" />
this will pass html validation
Even though i have specified no and false in checkbox value both are checked by default.
this is default behaviour for boolean values in HTML elements.
Only removing the checked attribute altogether will make the element not checked.
The background is in On SGML and HTML:
Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".
Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").
If you are purpose is validation and using jquery then you might need to use "change" event rather than "click".

What is the purpose of the `name` attribute in a checkbox input element?

I went through many online documents for the checkbox input in XHTML. Can anyone clear my doubt? What does this name field actually stand for?
Milk: <input type="checkbox" name="checkbox" value="Milk">
Chocolate: <input type="checkbox" name="checkbox" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox" value="Cold Drink">
I thought it was an identifier for that particular checkbox, which can later be used in other file by just referring their name, but given that all the checkbox had same name, why even specify it? A little confused of this.
Dont be confused because of name="checkbox". It could more logically be name="drink" and type=checkbox.
In the above case, you have multiple checkboxes with the same name. When several checkboxes have the same name, the form will send a group of values to the server in the request. Note: only the values of the checked checkboxes will be sent to the server.
Ideally these are used to allow multiple choice questions where more than one answer is allowed.
As opposed to radio buttons, where only one answer is allowed among the options.
Update:
On the receiving side, if you're using JSP for example - the values of the selected checkboxes will be available as request.getParameterValues("drink") or request.getParameterValues("checkbox") in your actual case. This is where the name attribute is used.
The name attribute is used to
reference form data after it’s
submitted, and to reference the data
using JavaScript on the client side.
Source: http://reference.sitepoint.com/html/input/name
Basically, what you've described. When the form is submitted, you can access the values of the form elements through the name you ascribe to them.
The only place where you would want to have multiple inputs with the same name is when they are radio buttons, in which case it is used to indicate which one of them belongs to the same group and thus only one of which can be selected at a time.
The name attribute is used to identify a checkbox. Which you could parse into a object like so {checkboxName1: 'checkboxValue2', checkboxName2: 'checkboxValue2'}
You missed the array setting for the name. By using the array setting (using square brackets), the result will be three different indexes for the checkboxes.
Milk: <input type="checkbox" name="checkbox[]" value="Milk">
Chocolate: <input type="checkbox" name="checkbox[]" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox[]" value="Cold Drink">
the "name" is same with the databse record, every field should have a name, so when you click the submit, the data will recorded to the database~~~~~