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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jonatecAuthor Commented:
Yeah that's brilliant, I like the use of cells to pick specific table cells, nice.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.