mercredi 30 septembre 2015

How to check if a checkbox is checked or unchecked using JavaScript? What is wrong in my code?

I am pretty new in JavaScript and I have the following problem. Into a JSP page I have something like this:

<tbody>
    <c:forEach items="${listaProgetti.lista}" var="progetto" varStatus="itemProgetto">
        <tr id='<c:out value="${progetto.prgPro}" />'>
            <td><input type="checkbox" class="checkbox" onchange="changeCheckbox(this, <c:out value="${progetto.prgPro}" />)"></td>
            .....................................................
            .....................................................
            .....................................................
        </tr>
    </c:forEach>
</tbody>

So, as you can see, clicking on the checkbox it is call this changeCheckbox() that have to discover if the clicked checkbox is checked or unchecked after the click event.

So I am trying to do something like this:

function changeCheckbox(elem, idRow) {
    alert("INTO changeCheckbox()");

    var checkedRowList = new Array();

    alert(idRow);   // Rappresenta il progetto attualmente selezionato
    //alert(elem);

    if(this.checked) {
        alert("CHECKED");
    } else {
        alert("UNCHECKED");
    }
}

So as you can see the first parameter passed to the changeCheckbox() function is the this object that should repreent the clicked input tag that represent a checkbox (correct me if it is a wrong assertion).

So into the script I try to check if the checkbox is checked or unchecked by if(this.checked) but it can't work

The problem is that when I check a checkbox don't enter in the body of the previous if but enter every time in the body od the else.

Why? What is wrong? What am I missing? How can I solve this issue?

Tnx




Aucun commentaire:

Enregistrer un commentaire