tags:

views:

151

answers:

1

If I have a checkbox like this in my jsp: <form:checkbox path="agreeToLegalAgreements" />

It results in: <input id="agreeToLegalAgreements1" name="agreeToLegalAgreements" type="checkbox" value="true"/><input type="hidden" name="_agreeToLegalAgreements" value="on"/>

Why is a "1" being appended to the id ? The reason I ask is because I have to hardcode the "1" if I want to select this checkbox using javascript: document.getElementById('agreeToLegalAgreements1').checked=true;

+2  A: 

This is necessary as you may need to bind multiple check boxes to the same field and each will need to have a unique id.

For example, if your form object has a List of interests

Programing: <form:checkbox path="interests" value="Programing"/>
Painting: <form:checkbox path="interests" value="Painting"/>
Fishing: <form:checkbox path="interests" value="Fishing"/>

The output will be:

Programing: <input id="interests1" name="interests" type="checkbox" value="Programing"/>
Painting: <input id="interests2" name="interests" type="checkbox" value="Painting"/>
Fishing: <input id="interests3" name="interests" type="checkbox" value="Fishing"/>

(I have omitted the hidden empty value input)

Donal Boyle