0

The checkbox should be checked, but it doesnt work. I´m baffled.

<script type="text/javascript">

function check() {
document.getElementById("1").checked = true;
}
</script>

<form>
<input id="1" type="checkbox" name="please check" value="1">please check<br>
</form>
7
  • You can't have an id that starts with anything other than a letter. Commented Apr 4, 2013 at 10:59
  • @boz You can in HTML5. Commented Apr 4, 2013 at 10:59
  • 4
    How do you call check function? Commented Apr 4, 2013 at 11:00
  • 2
    Please post the full code here.. I mean where are you calling the function check() etc.. Commented Apr 4, 2013 at 11:00
  • id="test" works neither Commented Apr 4, 2013 at 11:01

3 Answers 3

1

I have call it on Window Onload Where to call just pass function name

<script type="text/javascript">
    window.onload=check;

    function check() {
    document.getElementById("1").checked = true;
    }
    </script>

    <form>
    <input id="1" type="checkbox" name="please check" value="1">please check<br>
    </form>
Sign up to request clarification or add additional context in comments.

Comments

0

Order it the other way and get rid of the function, this will make sure that the checkbox exists when the script runs.

jsFiddle

<form>
    <input id="1" type="checkbox" name="please check" value="1">please check<br>
</form>
<script type="text/javascript">
    document.getElementById("1").checked = true;
</script>

Alternatively put it on document ready (jQuery) or window load.

jsFiddle

window.addEventListener('load', check);
function check() {
    document.getElementById("1").checked = true;
}

Comments

0

Try this:

<script type="text/javascript">

function check() {
document.getElementById("check1").checked = true;
}

window.onload = check;
</script>

<form>
<input id="check1" type="checkbox" name="please check" value="1">please check<br>
</form>

refer this link also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.