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>
jonatecAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Albert Van HalenConnect With a Mentor Analyst developerCommented:
2 options
1 : a key value pair as an array (for example values[11] = NewCastle)
2 : an array of objects => id and city
// 1
function fnUpdateTable1() {
	var values = [];
	$("#tb1 input:checked").each(function() {
		var cells = $(this).parents("tr:first").children();
		values[cells.eq(0).text()] = cells.eq(1).text();
	});
}

// 2
function fnUpdateTable1() {
	var values = [];
	$("#tb1 input:checked").each(function() {
		var cells = $(this).parents("tr:first").children();
		values.push({ "id": cells.eq(0).text(), "city": cells.eq(1).text()});
	});
}

Open in new window

0
 
Albert Van HalenAnalyst developerCommented:
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

0
 
jonatecAuthor Commented:
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)


       
    });
   
}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Albert Van HalenAnalyst developerCommented:
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

0
 
jonatecAuthor Commented:
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 ?
0
 
jonatecAuthor Commented:
Yeah that's brilliant, I like the use of cells to pick specific table cells, nice.
0
All Courses

From novice to tech pro — start learning today.