Link to home
Start Free TrialLog in
Avatar of jonatec
jonatecFlag for United Kingdom of Great Britain and Northern Ireland

asked on

jQuery to traverse a table

I want to trawl the table 'tb1' and grab the value of the first TD, (ie the ID number), if
the third TD for that row, checkbox element is checked. (I will then build a list and use AJAX to update the table changes back to the database).

How do I collect these items? I got a bit bogged down tring to traverse the table !!

Cheers.

<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    $("#btnUpdateTable1").bind("click", fnUpdateTable1);
    $("#btnUpdateTable1").attr("value", "Update Table 1");
});
function fnUpdateTable1() {

    $("#tb1 > tbody > tr").each(function() {

        //  ????
       
    });
}
</script>
</head>
<body>
<table border='1' id='tb1'>
      <tbody>
            <tr>
                  <td>10</td>
                  <td>Birmingham</td>
                  <td><input type='checkbox' /></td>
            </tr>
            <tr>
                  <td>11</td>
                  <td>Newcastle</td>
                  <td><input type='checkbox' checked/></td>
            </tr>
            <tr>
                  <td>12</td>
                  <td>London</td>
                  <td><input type='checkbox' /></td>
            </tr>                                          
      </tbody>
</table>
<p>
<input type="button" id="btnUpdateTable1" />
</body>
</html>
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Use this
All values (the contents of the first table-cell) are pushed into an array (values)
var values = [];
$("#tb1 input:checked").each(function() {
	values.push($(this).parent().prevAll(":last").text());
});

Open in new window

Avatar of jonatec

ASKER

If I put an alert in place to inspect the contents of the array, it show "11" three times, should it only collect "11" once ?

function fnUpdateTable1() {

    $("#tb1 > tbody > tr").each(function() {

        var values = [];
        $("#tb1 input:checked").each(function() {
                values.push($(this).parent().prevAll(":last").text());
        });

alert(values)


       
    });
   
}
Remove your first each function
My solution was to process each checked checkbox within the table
Your solution includes the processing of each table row and therefore pushing the value 11 into the array 3 times (3 rows)
function fnUpdateTable1() {
	var values = [];
	$("#tb1 input:checked").each(function() {
		values.push($(this).parent().prevAll(":last").text());
	});
}

Open in new window

Avatar of jonatec

ASKER

Thanks great thanks.

Lastly then, what if I wanted to include not only the ID in the array but also the City Name, how would do that ?
ASKER CERTIFIED SOLUTION
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jonatec

ASKER

Yeah that's brilliant, I like the use of cells to pick specific table cells, nice.