So, I have a JSP page that's attached to a list of objects I need to loop through using a JSTL loop. (This is a Tiles project and the controller is setting up some mock objects which are being added to and returned as a list. That bit works fine.) Basically, I want it to display a name, a value, and a button all in a row, and when the button is clicked, I want the rest of the info to pop down.
<c:forEach var="i" varStatus="curr" items="${items}">
<tr>
<td>
${i.itemName}
</td>
<td>
${i.itemValue}
</td>
<td style="float:right;">
<input type="button" id="showhide">
</td>
</tr>
<tr>
<td class="toggle">
${i.itemAltName}
${i.itemAdditions}
${i.itemValueAfterAdditions}
</td>
</tr>
</c:forEach>
I'm using the following JS.
<script type="text/javascript">
var buttons = document.getElementById('showhide');
buttons.onclick = function() {
var div = document.getElementsByClassName('toggle');
var x = div.length;
for(var i = 0; i < x; ++i){
if (div[i].style.display !== 'none') {
div[i].style.display = 'none';
}
else {
div[i].style.display = 'block';
}
}
};
</script>
Also, stating the obvious here, but my style area has:
.toggle{
display:none;
}
Currently, only the first button opens the corresponding text properly. From other answers I've seen, I know I need to make the ID unique, but I'm not sure how to do that with the JSP loop. Help?