input type = checkbox problem - html

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".

Related

I want to know why this input element is checked but the view is not match

when i creat a webpage with Vue,something happen,in this picture.
The checked attribute represents the default, not the current, value.
It doesn't update as the user interacts with the UI.
This is a possible way to reproduce:
document.getElementById("foo").checked = false;
<label for="foo">Click me <input id="foo" type="checkbox" checked="checked"></label>
Your HTML element has a checked property which is usually true or false. You will need to refer that attribute in order to properly detect the value.

How to locate "::after" element in selenium locator?

I have few checkboxes on my page, with few of them checked by default.
Through my code, I need to handle unchecking of all text boxes on the page and then proceed.
The problem is the checkboxes (both checked and un-checked) have exactly the same HTML / DOM structure with same attributes / values except for the "::after" appearing when the checkbox is checked.
How do I write a locator to find out if the element is checked or unchecked, and then proceed to uncheck it.
<!-- When checkbox is unchecked -->
<label class="someclasslabel" on-click="[[event]]">
<span>Male</span>
<input type="checkbox" data-bind="checked: $properties.value">
<span class="checkbox"></span>
</label>
<!-- When checkbox is checked -->
<label class="someclasslabel" on-click="[[event]]">
<span>Male</span>
<input type="checkbox" data-bind="checked: $properties.value">
<span class="checkbox">
::after
</span>
</label>
I want a locator, so that I can get attribute if its checked or unchecked, and then uncheck it if already checked.
You could try executing some Javascript on your checkbox element to get the value.
after_value = driver.execute_script
("return window.getComputedStyle(document.querySelector('.someclasslabel'),':after')
.getPropertyValue('content');")
This is a bit of a hack, but worth a try.
After getting all the checkbox elements (with the same selector) no matter if they are checked or not, you can then iterate through them with a loop, and check if they are checked with the selected method. (C# reference here https://seleniumhq.github.io/selenium/docs/api/dotnet/ for RemoteWebElement.Selected Property).
If .selected == true -》click; if not selected, do nothing;

What is this "checked id" attribute inside the <input> tag? Normally it is "id" attribute

I am looking at the sourcecode of a website, and come across the following code snippets
<input type="checkbox" name="age" value="1" checked id="age"><label for="age">I am an adult and capable citizen</label>
what is the attribute "checked id" as oppose to the "id" attribute that we normally see inside the tag, thanks.
checked is an attribute you place to make a checkbox 'ticked' by default.
id is a different attribute. the id of the element you posted is 'age'.
There is no such thing as checked id.
might as well take the word checked and place it just before the closing > of your input. same effect, but readable.
All fine, look at it closely. input type="checkbox", so it may have "checked"
There is nothing like chekced id. id is unique identifier and checked is default value for type checkbox. You can do something like
<input type="checkbox" id="age" name="age" value="1" checked ><label for="age">I am an adult and capable citizen</label>

What's the proper value for a checked attribute of an HTML checkbox?

We all know how to form a checkbox input in HTML:
<input name="checkbox_name" id="checkbox_id" type="checkbox">
What I don't know -- what's the technically correct value for a checked checkbox? I've seen these all work:
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked>
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="on">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="yes">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="checked">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="true">
Is the answer that it doesn't matter? I see no evidence for the answer marked as correct here from the spec itself:
Checkboxes (and radio buttons) are on/off switches that may be toggled
by the user. A switch is "on" when the control element's checked
attribute is set. When a form is submitted, only "on" checkbox
controls can become successful. Several checkboxes in a form may share
the same control name. Thus, for example, checkboxes allow users to
select several values for the same property. The INPUT element is used
to create a checkbox control.
What would a spec writer say is the correct answer? Please provide evidence-based answers.
Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:
<input name=name id=id type=checkbox checked=checked>
For HTML, you can also use the empty attribute syntax, checked="", or even simply checked (for stricter XHTML, this is not supported).
Effectively, however, most browsers will support just about any value between the quotes. All of the following will be checked:
<input name=name id=id type=checkbox checked>
<input name=name id=id type=checkbox checked="">
<input name=name id=id type=checkbox checked="yes">
<input name=name id=id type=checkbox checked="blue">
<input name=name id=id type=checkbox checked="false">
And only the following will be unchecked:
<input name=name id=id type=checkbox>
See also this similar question on disabled="disabled".
HTML5 spec:
http://www.w3.org/TR/html5/forms.html#attr-input-checked :
The disabled content attribute is a boolean attribute.
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
Conclusion:
The following are valid, equivalent and true:
<input type="checkbox" checked />
<input type="checkbox" checked="" />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="ChEcKeD" />
The following are invalid:
<input type="checkbox" checked="0" />
<input type="checkbox" checked="1" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="true" />
The absence of the attribute is the only valid syntax for false:
<input />
Recommendation
If you care about writing valid XHTML, use checked="checked", since <input checked> is invalid XHTML (but valid HTML) and other alternatives are less readable. Else, just use <input checked> as it is shorter.
<input ... checked />
<input ... checked="checked" />
Those are equally valid. And in JavaScript:
input.checked = true;
input.setAttribute("checked");
input.setAttribute("checked","checked");
you want this i think:
checked='checked'
checked
checked=""
checked="checked"
are equivalent;
according to spec checkbox
'----ⓘ checked = "checked" or "" (empty string) or empty
Specifies that the element represents a selected control.---'
It's pretty crazy town that the only way to make checked false is to omit any values. With Angular 1.x, you can do this:
<input type="radio" ng-checked="false">
which is a lot more sane, if you need to make it unchecked.
I think this may help:
First read all the specs from Microsoft and W3.org.
You'd see that the setting the actual element of a checkbox needs to be done on the ELEMENT PROPERTY, not the UI or attribute.
$('mycheckbox')[0].checked
Secondly, you need to be aware that the checked attribute RETURNS a string "true", "false"Why is this important? Because you need to use the correct Type. A string, not a boolean. This also important when parsing your checkbox.
$('mycheckbox')[0].checked = "true"
if($('mycheckbox')[0].checked === "true"){
//do something
}
You also need to realize that the "checked" ATTRIBUTE is for setting the value of the checkbox initially. This doesn't do much once the element is rendered to the DOM. Picture this working when the webpage loads and is initially parsed.
I'll go with IE's preference on this one: <input type="checkbox" checked="checked"/>
Lastly, the main aspect of confusion for a checkbox is that the checkbox UI element is not the same as the element's property value. They do not correlate directly.
If you work in .net, you'll discover that the user "checking" a checkbox never reflects the actual bool value passed to the controller.
To set the UI, I use both $('mycheckbox').val(true); and $('mycheckbox').attr('checked', 'checked');
In short, for a checked checkbox you need:
Initial DOM: <input type="checkbox" checked="checked">
Element Property: $('mycheckbox')[0].checked = "true";
UI: $('mycheckbox').val(true); and $('mycheckbox').attr('checked', 'checked');
Just like all input's, this one also has a 'value' attribute. If you set the value attribute along with the name attribute, it will send ${name}=${value} in the headers. Let's say we had a form with a bunch of stuff and we wanted the user to decide what roles they have, we could use a checkbox. The easiest way is to set the 'value' attribute which will be passed along to the server. Like this:
<form action="/server" method="post">
<label>Are you a person?</label>
<input type="checkbox" name="my_checkbox" value="is_person">
</form>
The server will receive the following:
my_checkbox=is_person
but only if the checkbox is checked. This would be
my_checkbox=
if it is empty.
This is the exact same with radio inputs.
But.. I have a feeling this isn't what you're asking for... So just encase I'll write another answer below:
If the checked attribute is set to anything (even false) it will be activated, no matter what. The checked attribute doesn't have a specified value, if it's set to anything it will default to 'true' (even if you set it to false).
For example:
<form action="/server" method="post">
<label>Are you a person?</label>
<input type="checkbox" checked="false"> <!--Still checked-->
</form>
Will be checked even though it's set to false.
<form action="/server" method="post">
<label>Are you a person?</label>
<input type="checkbox" checked="asjdiasjiasdjoasi"> <!--Still checked-->
</form>
Doing this will also check it. It doesn't matter -- as long as it's set it will be checked.
Hope this answers your question, the checked attribute will check the input no matter the value of the attribute.
You can test it here: https://battledash-2.github.io/Live-IDE/
or anywhere like repl.it, or locally.
The technically correct value for a checked checkbox is zero (0), and when the checkbox is not checked, it's index is not defined.
Well, to use it i dont think matters (similar to disabled and readonly), personally i use checked="checked" but if you are trying to manipulate them with JavaScript, you use true/false

How do I get the result of a checked checkbox?

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.