anon
anon

Reputation:

How to check/uncheck a checkbox while clicking a table row

I need to check/uncheck a checkbox that exist inside a row under table body when it is clicked anywhere within a row except a link. I tried to make it working by using jQuery's prop, but something is not working properly.

I have a table structure like below:

<table id="anywhere_click">
<thead>
    <tr>
        <th><input type="checkbox" onclick="selectAll('myDataID[]');" /></th>
        <th>Title</th>
        <th>Details</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><input type="checkbox" name="myDataID[]" value="1" /></td>
        <td>Stackoverflow</td>
        <td>Some text here....</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="myDataID[]" value="2" /></td>
        <td>Google</td>
        <td>
            <a href="aaa.html">This is a Link</a><br />
            Some text here....<br />
            <img src="something.jpg" />
        </td>
    </tr>
    <tr>
        <td><input type="checkbox" name="myDataID[]" value="3" /></td>
        <td>test</td>
        <td>Some text here....</td>
    </tr>
</tbody>
</table>

Upvotes: 0

Views: 1963

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

$('#anywhere_click tbody tr').click(function (e) {
    if(!$(e.target).is('#anywhere_click td input:checkbox'))
    $(this).find('input:checkbox').trigger('click');
});

$('#anywhere_click tr a').click(function (e) {
    e.stopPropagation();
});

Upvotes: 6

Mirko Cianfarani
Mirko Cianfarani

Reputation: 2103

If you checked when the site 's loading.

You have to add in the tag <td><input type="checkbox" name="myDataID[]" value="3" /></td> checked.

<td><input type="checkbox" name="myDataID[]" value="3" checked /></td>

With the button you want checked and unchecked.

Info: Information

and example: Examble with button checked and unchecked

Upvotes: 0

Related Questions