0

This is my HTML code:

<a id="id_1" name="n_1" href="#" onclick="callFn(this.id);">
    <input type="checkbox" name="ck_1" id="xyz">
    Click Here
</a>

I want to check this checkbox using Javascript code. How to do it??

User2403
  • 173
  • 1
  • 4
  • 15

3 Answers3

1

Use <label> instead of <a>.

<label id="id_1" onclick="callFn(this.id);">
    <input type="checkbox" name="ck_1" id="xyz">
    Click Here
</labek>

This is the use of <label> tag. And moreover, you don't need callFn(this.id); at all.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Get the element using its ID:document.getElementById("xyz")

Then change its property checked to true:

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

Minderov
  • 521
  • 1
  • 5
  • 20
  • 2
    your answer may be correct, but please add a bit more so beginners can understand what you are coming to. The community wants to help you, and we are trying are best, but we can only understand questions that make sense. Try to format your question a bit differently after you check out StackOverflow's Help Center page on how to write a good answer. – OneStig Jan 20 '16 at 23:11
0

Here is simple DEMO.. hope it helps

    $("#check").click(function() {
        var checkBoxes = $("#xyz");
        checkBoxes.attr("checked", !checkBoxes.attr("checked"));
    }); 
<label id="id_1" onclick="callFn(this.id);" for="xyz">
    <input type="checkbox" name="ck_1" id="xyz">
    Click Here
</labek>
<br/>
<button id="check">By button</button>
liborza
  • 969
  • 1
  • 7
  • 28