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

brgdotnet
brgdotnet used Ask the Experts™
on
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;
}
Comment
Watch Question

Do more with

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

are you passing the correct id into your function?

Test example below:
<html>
<head>

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

</body>
</html>

<script>
    showInfo('Checkbox1');
    showInfo('Checkbox3');
</script>

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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