My requirement is to have 3 check-boxes in each table row, and once the checkbox is checked, it will create a hidden input with the value corresponding to the first <td>
of THAT particular row.
Here is a sample row:
<tr>
<td>user1</td>
<td>768.51</td>
<td>4,680</td>
<td>0</td>
<td>0%</td>
<td>0</td>
<td><input type="checkbox" name=">1 year and <=2 years[]" onclick="createUserNameInput()"></td>
<td>0.00</td>
<td>0.00%</td>
<td>1</td>
<td><input type="checkbox" name=">2 years and <=3 years[]" onclick="createUserNameInput()"></td>
<td>768.51</td>
<td>100.00%</td>
<td>4,677</td>
<td><input type="checkbox" name="> 3 years[]" onclick="createUserNameInput()"></td>
</tr>
Once any of the 3 check-boxes are checked, this hidden input should be created. If 3 are checked, 3 inputs should be created. <value>
of these inputs should equal the value of first <td>
in THIS row, which is user1
. Here is how I am trying to implement it (code is inconsistent but I just need to get it working first):
<script>
function createUserNameInput() {
// var input = document.createElement("input");
// input.setAttribute("type", "hidden");
// input.setAttribute("name", "user");
// input.setAttribute("value", $(this).parent().siblings(":first").text()); // VALUE IS EMPTY
// document.getElementById("main-form").appendChild(input);
alert($(this).parent().siblings(":first").text()); // VALUE IS EMPTY
}
</script>
I have referenced these previous posts but none of those solutions work for me. When I run alert()
, value is empty - nothing is displayed:
Who can help? If it can be done without jQuery, even more amazing!