Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the checkbox unchecked by default always

Tags:

html

checkbox

I have 2 checkboxes inside a form and both those checkboxes were wrapped inside a form.

For one of form I have added the attribute autocomplete="off".
Below is the code snippet

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">     <head></head>     <body>         <form name="chkBoxForm" >             <div>                 <input type="checkbox" value="100" name="product"/>My Checkbox1             </div>         </form>         <form name="chkBoxForm" autocomplete="off">             <div>                 <input type="checkbox" value="200" name="product"/>My Checkbox2                                      </div>         </form>     </body> </html> 

Now my problem here if we check those checkboxes manually and if we press F5, then the checkbox with attribute autocomplete="off" got unchecked. But the checkbox which doesn't have that attribute remains checked. This happens in FireFox 22.

This behavior varies from Browser to Browser.
In IE, both the checkboxes remains checked and in Chrome both the checkboxes were unchecked.

But when I press enter in the address bar, it gets unchecked in all the browsers.

Can someone tell me how to have those checkboxes unchecked always when we press F5?
I am aware that this can be handled through Javascript.

Is there any other html way of handling this?

like image 867
Vel Avatar asked Jul 18 '13 19:07

Vel


People also ask

How do I default a checkbox to unchecked?

If you wanted to submit a default value for the checkbox when it is unchecked, you could include an <input type="hidden"> inside the form with the same name and value , generated by JavaScript perhaps.

Why is checkbox checked default?

The Input Checkbox defaultChecked property in HTML is used to return the default value of checked attribute. It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.

How do I make a checkbox automatically checked?

Add checked = "checked" to the input you want selected. For XHTML the recommended way is as described. Check HTML manual and W3C to confirm. The markup posted isn't XHTML, so it's logical to use just the simple keyword attribute checked .

How do I uncheck a checkbox in CSS?

The simple answer is NO, CSS cannot help you uncheck the checkbox.. You can use CSS to detect whether the input element is checked or not by using :checked and :not(:checked) .. By definition it shouldn't.


1 Answers

No, there is no way in simple HTML. Javascript might be your only solution at this time..

Loop through all inputs in javascript, check if they're indeed a checkbox and set them to unchecked:

var inputs = document.getElementsByTagName('input');  for (var i=0; i<inputs.length; i++)  {   if (inputs[i].type == 'checkbox')   {     inputs[i].checked = false;   } } 

wrap it up in a onload listener and you should be fine then :)

like image 161
David Fariña Avatar answered Oct 18 '22 14:10

David Fariña