Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox default checked="checked" is not working?

checkbox default checked is not working! I tried to fix it, but I can not find where is the error here? so on page load that is checked, after page loadet that is unchecked!? I tried that

<div class="onoffswitch" style="margin: 0 auto;">
<input type="checkbox" class="avacheckbox onoffswitch-checkbox" id="AvButtonAutoGames" checked="checked"/>
<label class="onoffswitch-label" for="AvButtonAutoGames">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
</label>
</div>

and that

  <div class="onoffswitch" style="margin: 0 auto;">
    <input type="checkbox" class="avacheckbox onoffswitch-checkbox" id="AvButtonAutoGames" checked/>
    <label class="onoffswitch-label" for="AvButtonAutoGames">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
    </div>
like image 676
Leo Avatar asked Dec 04 '18 22:12

Leo


People also ask

How do I keep a checkbox checked by 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 you check checkbox is checked or not?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.

How do I set a default checkbox value?

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.

How do I make check box checked?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.


2 Answers

To specify the value, use the checked="true", otherwise do not specify the property.

Checked: <input type="checkbox" checked="true" /><br/>
Not checked: <input type="checkbox" />
like image 137
jordiburgos Avatar answered Oct 30 '22 05:10

jordiburgos


It's hard to see, but you're using this syntax to define the checkbox value?

document.getElementById('AvButtonAutoGames').checked = true

If so, this isn't correct - but in the first JS example you have a correct the prop usage:

$('#AvButtonAutoGames').prop('checked', true)

as in HTML5, the syntax to apply is simply <input checked> or to expand for clarity <input checked="checked">. The absence of the checked attribute leads to an unchecked input field.

Simply convert your .checked=X to the functional calls and it should work

like image 45
Glycerine Avatar answered Oct 30 '22 03:10

Glycerine