html: how to uncheck a checked checkbox - html

I have an html form with a checked checkbox:
<input type=checkbox name="somebox" value="somevalue" checked />
When I get this form, uncheck the checkbox and post the form again, the checkbox is still being passed on together with submitted data. So, unchecking in does nothing finally.
Is there a way to uncheck it? Or may be there is another way to indicate that the checkbox is checked (without using "checked" keyword)?
Thanks in advance
UDP
Finally it was a bug in another place. Checkbox logics is all right. Thanks everyone.

If you have unchecked the checkbox, the "somebox" will not be passed a request parameter.
You must have made a mistake somewhere else.

You could use javascript or jQuery to uncheck it but that's hardley solving the problem.
Sounds like your fighting with viewstate and you'll either need to place this in an update panel, or out side the panel depending on needs, or in your code behind set the checked value of the control before you come back to the page.
get rid of the "checked" property in the control might also be a good start as everytime it comes back from a post it will try to check the box. instead, set it's checked state in the code behind.

If its not checked the name may be passed (often libraries do this when building a request) but the value will not, so if the value of somebox is not somevalue, its not checked.
See How come checkbox state is not always passed along to PHP script?

Related

How to check html form if its re-dirtied?

(using angularJs 1.6)
I have a form that has some controls, and a save button.
When I click save, i check to see if the form is dirty.
If its dirty, then i save the changes off to a database, then set the form.dirty to false.
I also have if form is dirty, and user tries to navigate away without saving, there is a warning message that the form has not been saved.
If i navigate away after saving and come back everything works as expected.
But if I change something, then save. Then change something else. The form does not go dirty without refreshing first.
Without seeing any code, I can only answer based on what you mentioned and therefore you shouldn't be changing the form's $dirty property manually.
You should use the $setPristine(); method instead.
As mentioned in AngularJS Docs:
Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.
Which sounds like what you are attempting to do.

How to make the difference between unckecked and disabled HTML checkboxes?

I'm trying to get the values from some checkboxes. Some are unchecked, some are checked but disabled. In both these cases, no POST data will be sent to the server by the browser.
What I'm wondering, is how can I know that the checkbox was unchecked, or disabled but checked?
You can only check, if a checkbox was checked (send) or not (not send).
If you realy want to know this server side, you can make use of AJAX or set a hidden input before submit.
You can check the state of the checkbox here
document.getElementById("myBox").disabled
Unfortunately the disabled checkbox values are never sent to POST. What I would do is make a small JS code to enable the checkboxes right before the form is sent, or use hidden fields which mimic the checkboxes' values.

Disable browsers Form inputs prefill/autofill feature when hitting "back" button

I want to "prevent browsers from prefilling form inputs when hitting the "back" button". Indeed, I want the initial values to be filled in (added via jsp), not the browser's (cached) values.
Solution 1: I found out that this can be done by disabling the browser caching for the current page. This seems a rather extreme solution considering that I "only" want to disable this prefill feature for a "form" (hence disable caching for the form only, not the whole page).
Solution 2: Then, the obvious next solution is to use javascript: that is, store the initial value in a data-* attribute, then, on page load, replace the input value by the initial value if they differ.
Both solutions seem rather unperfect (these are rather work arounds) I turn to you guys hoping to hear of a better one.
Resources:
How does SO's form remember previous input values?
Disable Firefox's Auto-fill
Pressing back prefills inputs with value from right before submit
HTML form values and 'Back' button
The first thing that comes to my mind is to use a <input type="reset"> button. These aren't seen often nowadays because the user rarely actually wants to reset the form, but here it might just be what you need.
You could also do it in javascript on page load with form.reset(); instead of providing a button for the user.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement.reset
This is similar to your solution 2 and thus still a workaround of the browser behavior, but it is a(n old) part of standard forms and I think it'll do the trick with very little additional code (no need for data-* attributes), so wanted to throw it out there.

how to save the state of radio buttons in htmlunit

I am using htmlunit to select radio button, and after clicking this button i checked if it is selected and got true which is fine.
my problem is when I leave the page and then back to it the radio button is not checkd and the default one will be the checked one !
does any one has idea about this issue ?
If somebody needs solution to this, you need to save the state to HtmlPage. When you want to resume to earlier radio button selection state, can achieve that using following code
HtmlPage selectionsHtmlPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
in future, at any point when you want to start over from the selection again,
webClient.getCurrentWindow().setEnclosedPage(selectionsHtmlPage);
This method is just a way to do that operation only when doing this does not affect/break the ideal expected workflow of the web application.

To change checkbox text or to not change?

I'm having an argument with a co-worker, and I'm trying to convince him that it's a bad idea to change checkbox text (label) according to the checkbox state.
For example, we have a combobox that automatically picks selected value (and is disabled) when checkbox next to it is checked and is enabled when checkbox is cleared. His idea is to show Autoselect when checkbox is checked and Manual select when it's cleared. I'm sure that this will confuse the user as users tend to think that checking a checkbox next to a verb will make it true, only to find that the label has changed to something else.
What is your opinion on this matter?
P.S. I remember reading about changing checkbox text somewhere, in a book or blog article, but can't remember where. It would be great to have this in writing :-)
No need to mess with something that already works.
Changing the label would be horribly confusing and counter-intuitive, especially in the way you describe.
If the label describes the current state ("Manual select"), it will not be clear whether it is necessary to click on the checkbox to achieve the described state (like with a button), or whether it already is in that state.
A checkbox is a simple thing. When it is off, it does A. When it's on, it doesn't do A. The label needs to reflect what it does when it's turned on; it is then perfectly clear what doesn't happen when it is turned off.
If you want to show that the choice is between "Auto select" and "Manual select" then you should either reword your checkbox label or have a radio button:
o Manual Select
* Auto Select
Which isn't really ideal either.
But you shouldn't be changing the text on the label - the user won't remember what it was before and will be confused when it changes.
Personnaly i would say that the label shouldn't change.
From my experience changing the text is confusing, I have the same issue with modal buttons that change their text. It is hard to tell whether pushing the button will turn on the selected state or the text is the selected state.
Tick box text should not change.
Changing the text when the checkbox is checked changes the meaning. If it's checked and the label says Autoselect, I expect the Autoselect option to be enabled. If you uncheck, it's clear that the Autoselect option is NOT enabled. If you change the text to be Manual select whenever the checkbox is cleared I would expect that checking the box would enable the Manual select option. It's way too confusing if the text keeps changing.
You're right, he's wrong.
Checkbox labels should not change. That's part of the way checkboxes work.
See Should “toggle buttons” show what they do or the system status? on UXExchange for an alterantive approach using toggle buttons instead of checkboxes.
The label definitely shouldn't change. It's not only confusing, but it's unnecessary and ugly.
The only way I can think to make it less confusing would be to have the label state in brackets the current state, i.e. "Manual(currently auto)[]".
But that's really just a testiment to how ugly it gets.
Don't mess with what people are used to. Especially when you consider that if you do this, one will always be shown WITH a checkmark, and the other WITHOUT. Really confusing.
In the name of compromise, you COULD use a button instead, which toggles the control back and forth, and changes its value with each click.
Current mode: [Manual]
Still kind of confusing though. As I said, don't mess with what people are used to. It's like if you put a scrollbar on the left of the screen. It'll just mess with peoples' heads.
I would not change the checkbox label... I would have it say "automatic." But, depending on what your controls are actually doing, I might modify the selection in the grayed-out combo-box to reflect that it is now under automatic control.
Then to make the logic go both ways, if while the checkbox was unchecked, the user selected the "automatic" value in the combo-box, the checkbox would then become checked.
Or in another scenario:
If, when the "automatic" checkbox is checked, the automatic logic actually chooses a default value that is in the combo-box, then change the selection in the combo-box to show what the automatic setting actually is. The combo would not be editable, but it could be used to provide information about the actual setting that is being engaged.