How to check if a checkbox in a checkbox array is checked with jquery
Posted
by
eddy
on Stack Overflow
See other posts from Stack Overflow
or by eddy
Published on 2010-12-24T21:27:17Z
Indexed on
2010/12/24
23:54 UTC
Read the original article
Hit count: 535
Hello everyone.
I have a html table with a column of text boxes (mileage), all of them are disabled when the page loads, and I need that when user clicks on a check box, the text box for that column should be enabled and become a required field, and then if the user unchecks the checkbox, the textbox on that row must be disabled and the required class removed.
I already have the function to enable and disable the text boxes (column mileage) when the user clicks on the corresponding checkbox, but it is written in javascript.However, for some reason it only works in IE).
Here is the html code
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectItems" value="<c:out value="${item.numberPlate}"/>" onchange="enableTextField(this)" />
</td>
<td align="left"><c:out value="${item.numberPlate}"/></td>
<td align="left"><c:out value="${item.driver.fullName}"/></td>
<td align="left"><input type="text" name="mileage_<c:out value="${item.numberPlate}"/>" value="" disabled="true"/></td>
</tr>
</c:forEach>
</tbody>
And the javascript code:
function enableTextField(r)
{ var node = r.parentNode;
while( node && node.tagName !== 'TR' ) {
node = node.parentNode;
}
var i=node.rowIndex;
if(document.form1.selectItems[i-1].checked)
{
document.getElementById('mileage_' + document.form1.selectItems[i-1].value).disabled=false;
}
else
{
document.getElementById('mileage_' + document.form1.selectItems[i-1].value).value="";
document.getElementById('mileage_' + document.form1.selectItems[i-1].value).disabled=true;
}
}
Now, I know that in order to add or remove dynamically validation rules I have to use: addClass('required'); o removeClass('required') ,but I don't know how to detect whether a check box is selected or not, and based on that ,enable or disable the text box for that row.
I really hope you can help me out with this.
© Stack Overflow or respective owner