87

For the life of me I can't set my CheckBox to checked at page load. I have tried adding value="true" and Checked="checked" after id and still nothing. Any ideas?

<input type="checkbox" class="onoffswitch-checkbox" id="inline">
3
  • 5
    Add the checked attribute like this <input type="checkbox" class="onoffswitch-checkbox" checked id="inline"> Commented Dec 10, 2017 at 21:26
  • 4
    checked="checked" was correct. Shorter variant is just checked. Commented Dec 10, 2017 at 21:26
  • 3
    This question is similar to: What's the proper value for a checked attribute of an HTML checkbox?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Mar 19 at 18:00

9 Answers 9

153

just write "checked" and it works

 <input type="checkbox" class="onoffswitch-checkbox" id="inline" checked> 
Sign up to request clarification or add additional context in comments.

Comments

22

added "true" parameter below, just try it before, and it works for me

<input type="checkbox" class="onoffswitch-checkbox" id="inline" checked="true"> 

2 Comments

This works wonders if you have a styled checkbox where the true input is hidden. Thanks!
using "true" might be misleading. because you can't set the value to "false" and get the opposite result. only the attribute being there is important. not the value and people tend to set checked="" if it needs to have xml syntax.
8

The attribute checked="checked" does work and the shorthand notation is just checked.

<input type="checkbox" class="onoffswitch-checkbox" id="inline" checked="checked"/>

Comments

5

I tried all above solutions and nothing worked. It did give me some terminology to continue my research. What I ended up doing was going to the bottom of the code and added $('#inline').prop('checked', true); This solved my problem.

2 Comments

Echoing that this was what worked for me. The checked attribute pre-set in HTML does not work for some reason.
jquery for life. It's funny, some approaches will essentially be, never use jquery, unless you want to make sure something works.
1

You're looking for the checked content attribute. Here's the relevant snippet from the official documentation: (Living Standard):

The checked content attribute is a boolean attribute that gives the default checkedness of the input element. When the checked content attribute is added, if the control does not have dirty checkedness, the user agent must set the checkedness of the element to true; when the checked content attribute is removed, if the control does not have dirty checkedness, the user agent must set the checkedness of the element to false.

Note the docs have links to definition of relevant terms.

If it seems too technical, you can always go to MDN page on checked. MDN contributors have put up considerable effort into "translating" HTML Standard into more readable/understandable form (often times with examples), especially useful to people without a solid foundation on web related terminology and concepts.

Note: You'll also find the direct answer to your question on MDN.

Comments

0

I think it's worth adding that setting the checked attribute will check the box and not allow it to be unchecked by the user. If the desired functionality is for the checkbox to be checked by default but still able to be unchecked by the user, use the defaultChecked attribute:

<input type="checkbox" id="inline" defaultChecked />

or

document.getElementById("inline").defaultChecked = true;

1 Comment

defaultChecked is useful e.g. if you are using a framework like React and you don't want to control the checked property, but would like to control the initial state of the input.
-1
 <input type="checkbox" checked /> 

Comments

-1

you can copy this code and paste your project.i solve problem.

<input type="checkbox" class="onoffswitch-checkbox" id="inline" checked>

input tag element in attribute for checked

Comments

-1

This can be done in several ways, First you can add "checked" attribute in <input> tag.

<input type="checkbox" class="onoffswitch-checkbox" id="inline" checked>

If this is not work then you can check the checkbox using javascript.

document.getElementById("inline").checked=true;

You can add this js code within a function (checkbox will check with function execution event) or at the end of the html file within <script> tag.

Or else you can use jQuery for this.

$("#inline").prop("checked", true);

This also you can use inside a function or document.ready block.

$( document ).ready(function() {
    $("#inline").prop("checked", true);
});

This will check the checkbox after completely load the page.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.