Rjmr
Rjmr

Reputation: 19

Getting array of IDs from html to Javascript

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>

Upvotes: 0

Views: 161

Answers (1)

duncan
duncan

Reputation: 31930

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

document.getElementsByName('deacTraining');

to:

document.getElementsByName('users');

Upvotes: 1

Related Questions