Anthony
Anthony

Reputation: 2416

Set the checkbox to checked for input fields in the first 2 table rows

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

Answers (2)

Bogdan Savluk
Bogdan Savluk

Reputation: 6312

Just slice them:

$("[type=checkbox]").slice(0,2).prop('checked', true);

Upvotes: 2

dfsq
dfsq

Reputation: 193291

You can use for example nth-child(-n+2) selector:

$('table tr:nth-child(-n+2) :checkbox').prop('checked', true);

http://jsfiddle.net/B8h7N/

Upvotes: 3

Related Questions