4
        <input type="checkbox" name="name[]" value="A">A</input>
        <input type="checkbox" name="name[]" value="C">C</input>
        <input type="checkbox" name="name[]" value="D">D</input>
        <input type="checkbox" name="name[]" value="E">E</input>

I have value A and C, how to use javascript make A & C checked

2
  • You should have shown us what you've tried until now or you should just have checked the duplicates of that question: stackoverflow.com/questions/8206565/… to find a possible solution of that specific situation. Commented Jun 24, 2015 at 17:14
  • Also, note that <input> elements are void elements - that means that they don't have a closing tag. Use a <label> to associate text with an input, and you can make your elements self-closing by adding a slash: <input type="checkbox" name="name[]" value="A" />. Commented Jun 24, 2015 at 17:22

5 Answers 5

3

This should work for you.

var myNodeList = document.querySelectorAll("input[value=A], input[value=C]");
for (i = 0; i <	myNodeList.length; i++) {
   	myNodeList[i].checked = true;
}
<input type="checkbox" name="name[]" value="A">A</input>
<input type="checkbox" name="name[]" value="C">C</input>
<input type="checkbox" name="name[]" value="D">D</input>
<input type="checkbox" name="name[]" value="E">E</input>

Sign up to request clarification or add additional context in comments.

Comments

1

Try

['A','C'].forEach(v=> document.querySelector(`[value=${v}]`).checked=1)

['A','C'].forEach(v=> document.querySelector(`[value=${v}]`).checked=1)

// and her is longer but theoretically more efficient version:
// document.querySelectorAll(['A','C'].map(v=> `[value=${v}]`)+'').forEach(c=>c.checked=1)
<input type="checkbox" name="name[]" value="A">A</input>
<input type="checkbox" name="name[]" value="C">C</input>
<input type="checkbox" name="name[]" value="D">D</input>
<input type="checkbox" name="name[]" value="E">E</input>

Comments

0

Why not just give the checkbox an id like:

   <input type="checkbox" name="name[]" value="A" id="checkA">A</input>

then use the following javascript:

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

1 Comment

I cant give each checkbox an ID because I have may be ten checkboxes with same name[]
0

You have to assign a id to the input box first then you can call javascript by using that id.

  <html>
    <script type="text/javascript">
    function setValue()
    {
       document.getElementById("A").checked = true;
       document.getElementById("C").checked = true;
    }
    </script>
    <body>
       <input id="A" type="checkbox" name="name[]" value="A">A</input>
       <input id="B" type="checkbox" name="name[]" value="C">C</input>
       <input id="C" type="checkbox" name="name[]" value="D">D</input>
       <input id="D" type="checkbox" name="name[]" value="E">E</input>
       <br/><br/>
       <button onClick="setValue()"> Set Value </button>
    </body>
  </html>

Comments

0

You can use

document.querySelectorAll("input[value=A]")[0].checked = true;
document.querySelectorAll("input[value=C]")[0].checked = true;

1 Comment

Note that document.querySelectorAll() returns an array of nodes, so you'd have to access them via index (eg. result[0]). That's probably the reason your answer wasn't accepted.

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.