user3555971 user3555971 - 6 months ago 11
jQuery Question

copy one column to another column based ibn condition in jquery

<table style="width:100%">
<tr>
<th>NAME</th>
<th> <input type="checkbox" name="" id="approvechk" value="1"> APPROVE</th>
<th><input type="checkbox" name="" id="disapprovechk"> DISAPPROVE</th>

</tr>
<tr>
<td approve>Jill</td>

</tr>
<tr>
<td approve>Jack</td>

</tr>
<tr>
<td disapprove>Doe</td>

</tr>
<tr>
<td>John</td>

</tr>
</table>


when I click the approve or disapprove checkbox the names which has corresponding approve or disapprove attribute should get filled on the respective APPROVE and DISAPPROVE columns. hoiw can I do this in a easy using Jquery.

Answer

Here is an implementation of what you ask for.

You can use :

$("#approvechk").prop("checked");

to see if your checkbox is checked or not. The return value is a boolean.

Then, I use nth-child selector to choose the right column :

$("table tr:nth-child("+tableIndex+") td:nth-child(2)").html($(value).text());

Keep in mind that nth-child index starts at one. So the table index starts at 2.

I am sure there is a cleaner way. This code works though, try it :

https://jsfiddle.net/mantisse_fr/g8rehfmh/

Comments