I have this html code:
<c:forEach items="${config.sources}" var="source" varStatus="sourceStatus">
<form:checkbox path="sources[${sourceStatus.index}].selected"
label="${source.sourceName}"
name="${sourceStatus.index}"
onclick="toggle(this)"/>
<br />
<c:forEach items="${source.feeds}" var="feed" varStatus="status">
<c:if test="${feed.display == true }">
<form:checkbox path="sources[${sourceStatus.index}].feeds[${status.index}].selected" name="${sourceStatus.index}"/>
<c:out value="${feed.name}" /> (provided by ${feed.provider})
<br />
</c:if>
</c:forEach>
</c:forEach>
What it does is make a bunch of checkboxes, where if the “main” checkbox becomes selected, it call the javascript method “toggle”, which should select all the “sub” checkboxes for that “main” checkbox. Here is the javascript:
<script type="text/javascript">
function toggle(chkbox) {
if (chkbox.checked) {
var x = document.getElementsByName(chkbox.name);
for ( var i in x) {
if (!x[i].checked)
x[i].click();
alert("HEY!" + x[i].name);
}
} else if (!chkbox.checked) {
var x = document.getElementsByName(chkbox.name);
for ( var i in x) {
if (x[i].checked)
x[i].click();
alert("HEY!" + x[i].name);
}
}
}
</script>
However, while the “main” checkbox get’s checked, none of its “sub” boxes get checked. Furthermore, when the alert goes off after the “main” has been selected, only ONE alert goes off, presumably for the “main” box, and it does not print out an index like expected, instead it says, “sources[X].selected” where X is the index. Furthermore, when deselected, it has four alerts, regardless of the number of “sub” boxes. They always say, “sources[X].selected”, “undefined”, “item”, and “namedItem”, respectively. WTH is going on??! I must be doing something very wrong.
Ok, so the problem for me was the jsp reserves almost every settable variable. Because of this, one cannot use name, or id, or anything like that to identify the checkboxes. What I ended up doing was this:
JSP:
Javascript:
Title was the only editable field that jsp did not use I could find. Sad, I know, but oh well.