How to check or uncheck a checkbox using the id of the checkbox

brgdotnet used Ask the Experts™
I have a java script function named showInfo(). Show info is passed the name/id of a specific check box control. Now I want to be able to check or uncheck the check box control based upon the control Id that I pass in to the Java script function? I have tried using getElementById but that isn't working. Does
anyone know how I can check the check box based upon the id of the check box control using Java Script?

function showInfo(idOfCheckBox)
    document.getElementById(idOfCheckBox).checked = true;
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
your function should work just fine.

are you passing the correct id into your function?

Test example below:

    function showInfo(idOfCheckBox) {
        document.getElementById(idOfCheckBox).checked = true;
    Test 1<input id="Checkbox1" type="checkbox" /><br>
    Test 2<input id="Checkbox2" type="checkbox" /><br>
    Test 3<input id="Checkbox3" type="checkbox" /><br>



Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

How are you calling your function?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial