0

I have used the display tag library to display my first returned results in JSP file. For further processing purpose, I need the selected checkbox values and manipulate them in the Javascript. However, I couldn't get them by using getElementsByName() function. Hope someone here can kindly give me some suggestions to proceed. Thanks a lot!

Following are my display table code in JSP file:

<display:table name="${List}" id="row" class="displaytable">
    <display:column title="Equipment" class="fieldAlignCenter">
          <c:forEach var="deviceRow" items="${row.getDeviceList()}">
              <input type="checkbox" name="equipNo" value="${deviceRow.equipNo}">
              <c:out value="${deviceRow.equipNo}" />
          </c:forEach>
    </display:column>
</display:table>
ShadowScorpion
  • 207
  • 6
  • 21

2 Answers2

0

Just make sure that you also assign an ID to the checkbox. This id can be the same as name.

<input type="checkbox" name="equipNo" id="equipNo" value="${deviceRow.equipNo}">
<script type="text/javascript">
alert(document.getElementById('equipNo').checked);
</script>
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Thanks Michael, I have added the ID to the checkbox while it still showed me the result is "undefined". Is there any declaration I miss there in Javascript? – ShadowScorpion Feb 22 '13 at 02:32
  • an checkbox doesn't have a value. It is checked or not checked. `` and in javascript: `document.getElementById('checkboxid').checked` this will be true if it's checked, false if it's unchecked. – Tschallacka Feb 22 '13 at 09:17
0

Add id to input tag like this

<display:table name="${List}" id="row" class="displaytable">
    <display:column title="Equipment" class="fieldAlignCenter">
          <c:forEach var="deviceRow" items="${row.getDeviceList()}">
              <input type="checkbox" id="equipNo_id" name="equipNo" value="${deviceRow.equipNo}">
              <c:out value="${deviceRow.equipNo}" />
          </c:forEach>
    </display:column>
</display:table>

and use document.getElementById("equipNo_id").checked

999k
  • 6,257
  • 2
  • 29
  • 32
  • Dear Toms, Thanks for your suggestions. While I have tested the result by changed the code same with your example. However, it still show me the alert "undefined" which is the same result with getElementsByName() function. – ShadowScorpion Feb 22 '13 at 02:31
  • Another concern is getElementById() function only provides the result of first element with specific id based on the documentation http://www.w3schools.com/jsref/met_doc_getelementbyid.asp – ShadowScorpion Feb 22 '13 at 02:42