0

how can i get and loop deacTraining value in JS ? in this given code, checkboxes's length is 0 after getting deacTraining's value.

html code snippet

<c:forEach var="acBatch" items="${batchBeanAll}" varStatus="loop">
<c:if test="${acBatch.getBatchStatus() == 'Active'}">
    <c:set var="counter" value="${counter + 1}" scope="page"/>
    <tr>
        <td scope='col'><input id="deacTraining${loop.index }" type='checkbox' name='users'value='${acBatch.getBatchCode()}'/></td>
        <td scope='col'>${acBatch.getBatchDescription()}</td>
        <td scope='col'>${acBatch.getBatchSize()}</td>
        <td scope='col'>${acBatch.getBatchStatus()}</td>
    </tr>
</c:if>

<input value="Continue" type="submit" onclick="return checkDeactivateTraining();"/>

js code

<script>
function checkDeactivateTraining(){
    var checkboxes = document.getElementsByName('deacTraining');
    for (var i=0; i<checkboxes.length; i++){
        if (checkboxes[i].checked)
            return true;
    }

    alert("Please select a training to deactivate.");
    return false;
            }
</script>

1 Answer 1

1

The name of your checkboxes is users, the id is 'deacTraining...'. Change:

document.getElementsByName('deacTraining');

to:

document.getElementsByName('users');
Sign up to request clarification or add additional context in comments.

4 Comments

gosh. did not realize this one. i'll try using getElementsById
@Rjmr - since ids are required to be unique, this would not make sense. Moreover, getElementById exists, whilegetElementsById (note the 's') does not. :)
yes, getElementsById does not exist. can you suggest an alternative way for this?
uh, just use getElementsByName, but fix the name you're using to actually reflect the name of your checkboxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.