Checking if a checkbox is Checked or not in js

Ok, I have a jsp where I have some checkbox elements put dynamically,


<form:form id="frm1" name="frm1">
<c:forEach var="appointmentSchedule" items="${appointmentSchedule}">
 <td>
<input name="a${appointmentSchedule.apptId}" type="checkbox"  id="${appointmentSchedule.apptId}" >
</td>
</c:forEach>
<input type="button" value="Confirm" onClick="checkCBoxes()"/> 
</form:form>

As you see I have the names and ids for the checkboxes generated dynamically for the checkboxes using ‘apptId’ values from each item in the ‘appointmentSchedule’ list.

What I am trying to do is check which of the checkboxes are checked and which are all unchecked. This is the js function I’m calling on clicking the ‘Check’ button.


function confirmAppts(tabNum){  
	var divEls = document.getElementsByTagName("input");
		var i = 0;
		for(i=0;i<divEls.length;i++)
		{
			if (divEls[i].type == "checkbox"){
				var tempName = divEls[i].name;
				alert(tempName);
				var tester = document.frm1.tempName.checked;
			        alert(tester);
			}
		}	
}

(I had been using those alerts to check and resolve this problem) . This js is however not working. I am getting the name of each of the checkboxes in the form correctly. And when I try using name with the statement to check if the corresponding checkbox is checked or not, I am not getting the ‘tester’ value alerted, which should either be a ‘true’ or a ‘false’ as expected.

To check if the " var tester = document.frm1.tempName.checked; " statement is correct, I just replaced the ‘tempName’ with valid hardcoded value of a checkbox name. This is it


 var tester = document.frm1.a404.checked;
alert(tester);

Now this gives me a ‘true’ or a ‘false’ based on whether the checkbox with name ‘a404’ is checked or not correctly. The ‘alert(tempName);’ throws up the same ‘a404’ but then, the ’ var tester = document.frm1.a404.checked; ’ is not giving a true or a false. I’m clueless on why this is happening. Please help me out with this.

Javascript works on the html code, not the jsp code.

What does the actual HTML code look like?


    function confirmAppts(tabNum){  
		var divEls = document.getElementsByTagName("input");
		var i = 0;
		for(i=0;i<divEls.length;i++){
			if (divEls[i].type == "checkbox"){
				if(divEls[i].checked ==  true)
					alert('checked');
				else
					alert('not checked')
			}
		}	
	}

And if you want to continue with your own way then i think you need to eval the line:


    function confirmAppts(tabNum){  
		var divEls = document.getElementsByTagName("input");
		var i = 0;
		for(i=0;i<divEls.length;i++){
			if (divEls[i].type == "checkbox"){
				var tempName = divEls[i].name;
				alert(tempName);
				var evalVar = 'var tester = document.frm1.' + tempName + '.checked;';
				eval(evalVar);
				alert(tester);
			}
		}	
	}

This is How it looks like.

<script language="javascript">
function checkCBoxes(){  
	var divEls = document.getElementsByTagName("input");
		var i = 0;
		for(i=0;i<divEls.length;i++)
		{
			if (divEls[i].type == "checkbox"){
				var tempName = divEls[i].name;
				alert(tempName);
				var tester = document.frm1.tempName.checked;
			        alert(tester);
			}
		}	
}
</script>
<form:form id="frm1" name="frm1">
<table class="formtable">
	<tr height="35"><td></td></tr>
	<tr>
<c:forEach var="appointmentSchedule" items="${appointmentSchedule}">
 <td>
<input name="a${appointmentSchedule.apptId}" type="checkbox"  id="${appointmentSchedule.apptId}" >
</td>
</c:forEach>
<input type="button" value="Confirm" onClick="checkCBoxes()"/> 
</tr>
</table>
</form:form>

FYI, I found that the problem is, the variable ‘tempName’ which I am using to store each of the checkbox elements name is not considered as variable in the ’ var tester = document.frm1.tempName.checked; ’ statement. Instead it is checking for a checkbox element with name tempName which is not actually present in the code.

Did you try the solution that i posted above?

Yes, I just tried and it is working super fine. Thanks a lot. I have used the first part of your solution. Can you please give some more information on the eval() ? What does it do?

w3schools says:

The eval() function evaluates and/or executes a string of JavaScript code.

First, eval() determines if the argument is a valid string, then eval() parses the string looking for JavaScript code. If it finds any JavaScript code, it will be executed.

An among other things, Mozilla’s JavaScript reference for eval says:

Don’t use eval!

eval() is a dangerous function, which executes the code it’s passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user’s machine with the permissions of your webpage / extension.

There are safe alternatives to eval() for common use-cases.

Yes I also don’t recommend it to use in easy cases. But sometimes it really becomes a handy function.

Thank You rajug and pmw57. Let me beware of eval() then. Anyways, as for this case, the first piece of your solution is working super fine. Thanks again.


var tester = document.frm1[ divEls[i].name ].checked;

Or, using code that is more appropriate today:


var form = document.getElementById('frm1');
...
var fieldName = divEls[i].name;
var tester = form.elements[fieldName].checked;

Is that more appropriate than the following in OP’s particular case?


	function confirmAppts(tabNum){  
        var divEls = document.getElementsByTagName("input");
        var i = 0;
        for(i=0;i<divEls.length;i++){
            if (divEls[i].type == "checkbox"){
                if(divEls[i].checked ==  true)
                    alert('checked');
                else
                    alert('not checked')
            }
        }   
    }

The OP’s code processes all inputs on the page, which is about the only difference that there is.

Thank You rajug and pmw57. Let me beware of eval() then. Anyways, as for this case, the first piece of your solution is working super fine. Thanks again.

Is my post a lucky one to start with. Just kidding, for you have just copy pasted a post I did earlier in this topic as your first post.:slight_smile: