Reputation: 2416
I want to set the checkbox to checked for input fields in the first 2 table rows of a table.
markup
<tbody>
<tr>
<td>
<input type="checkbox" id="" class="checkinput financeOption"><!--want this checked on page load-->
</td>
<td class="datatd">
<label id="">Option 1</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="" class="checkinput financeOption"><!--want this checked on page load-->
</td>
</td>
<td class="datatd"><label id="">Option 2 </label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="" class="checkinput financeOption">
</td>
<td class="datatd"><label id="">Option 3</label>
</td>
</tr>
I know I can use $( "").prop('checked', true); to set the property but struggling with figureing out how to target the first 2.
Thanks
Upvotes: 0
Views: 90
Reputation: 6312
Just slice them:
$("[type=checkbox]").slice(0,2).prop('checked', true);
Upvotes: 2
Reputation: 193291
You can use for example nth-child(-n+2)
selector:
$('table tr:nth-child(-n+2) :checkbox').prop('checked', true);
Upvotes: 3