I use JQuery and I have a web page which lists multiple rows with radio buttons and text boxes in each row. I am trying to enable the textbox when I click on the radio button in a row. What I am expecting is - it should enable/disable the text box only in that row. It shouldnt enable/disable the text boxes in other rows.
By default all text boxes in each row is disabled on page load. I researched and I was able get a solution to enable a textbox which when I click on a radio button. Now when I select another radio button i expect the previously enabled text box should get disabled again and the text box in the current row should now get enabled. This is what I am trying to achieve.
I did lot of research and most answers doesnt suit the requirements. Please kindly help. I use JQuery, I tried to get solution with Jquery and javascripts as well.
Here is the code:
<div class="content">
<form name="selectedMatIds" action="@{addMaterialsToBuffer()}" method="POST">
<div class="separator" style="margin-top:30px"><h4>Materials found</h4></div>
<table class="table MaterialTable" id="makeZebras">
<thead>
<tr>
<th class="alignLeft first">
Code
</th>
<th class="alignLeft">
Measure
</th>
<th class="alignLeft">
Unit
</th>
<th class="alignLeft">
New Unit
</th>
</tr>
</thead>
<tbody>
#{list items:foundList, as:'mat' }
<tr>
<td>
<input type="radio" name="selectedMatIds" id="selectedMatIds" value="${mat.id}" onclick="getMaterialID()"> ${mat.materialCode}
</td>
<td>
${mat.bum}
</td>
<td>
${mat.iumPerBum}
</td>
<td>
<div id="group">
<input type="text" id="newIUM" name="${mat.id}" value="${newIUM}" disabled>
<input type="hidden" id="oldIUM" name="oldIUM" value="${mat.iumPerBum}">
<input type="hidden" id="bum" name="bum" value="${mat.bum}">
<a id="save" onclick="loadURL1()">Save</a> <a>Cancel</a>
</div>
</td>
</tr>
#{/list}
</tbody>
<div class="actions">
<input type="submit" class="primary" value="Continue">
</div>
</table>
Now, the Jquery script:
$("input:radio[name='selectedMatIds']").click(function() {
var isDisabled = $(this).is(":checked");
var value = $(this).val();
var textValue = $("#newIUM").val();
alert(isDisabled);
alert(value);
alert(textValue);
if(isDisabled) $("#newIUM").removeAttr("disabled");
else $("#newIUM").attr("disabled", isDisabled);
});
.prop('disabled', isDisabled)instead of that attr/removeAttr mess. And to test if the element is checked:this.checked