How to get a value of the column selected by checkbox in JQuery?

advertisements

Currently I get all the output from the row. Is it possible to get the value of selected "Points" only. How can I do this?

Expected output when checked: SF01 SF02 SF03

Current output when checked: SF01 11.5 3.5328 120.3359

<table border="1" width="100%" id="selectTable" name="selectTable">
    <legend>Display Data Points</legend>
    <tr>
        <th></th>
        <th width="40%">Point</th>
        <th width="20%">Depth</th>
        <th width="20%">Lat</th>
        <th width="20%">Long</th>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="checkBox" name="checkBox[]" />
        </td>
        <td>SF01</td>
        <td>11.5</td>
        <td>3.5328</td>
        <td>120.3359</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="checkBox" name="checkBox[]" />
        </td>
        <td>SF02</td>
        <td>41.5</td>
        <td>6.5328</td>
        <td>113.3359</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="checkBox" name="checkBox[]" />
        </td>
        <td>SF03</td>
        <td>82.1</td>
        <td>5.2828</td>
        <td>721.9059</td>
    </tr>
</table>

<script type="text/javascript">
    function clickedBox() {

        var values = new Array();

        $.each($("input[name='checkBox[]']:checked").closest("td").siblings("td"), function() {
            values.push($(this).text());

        });

        document.getElementById("boxArea").value = "=====Results=====\n" + values.join("\n");
    }

    $(document).ready(function() {
        $("#selectPoint").click(clickedBox);
    });
</script>


Change siblings('td') to next('td') and I hope you need to bind event to checkbox.

$.each($("input[name='checkBox[]']:checked").closest("td").next("td"), function () {
                                                         //^^^^here
    values.push($(this).text());
});

DEMO

When you say siblings then it will fetch all the siblings of first td, which is why you are getting all the values.