Brendan Gooden Brendan Gooden - 1 year ago 237
Javascript Question

Jquery add data array to table rows

I have a Jquery code to loop through a table and select unique values, then populate another table with these unique values. My code is as follows:

var items = [], options = [];

//Iterate all td's in first column
$('#PartsTable tr td:nth-child(1)').each(function () {
//add item to array

//restrict array to unique fleet numbers
var items = $.unique(items);

//iterate unique array and build array of select options
$.each(items, function(i, item) {
options.push('<tr><td align="left" valign="top">' + item + '</td><td class="delete" align="center" style="background-color: transparent;"><i class="fa fa-times text-red cur-pon"></i></td></tr>');

//finally append the items from the array

However I'm getting an error on the line marked above - 'items is undefined'

When I run the debugger in Chrome,
var items = $.unique(items);
clearly returns an array with 2 values in it.

Can anyone shed any light on this? Thanks!

Answer Source

There is a typo here

var items = $.unique(items);

That last }); shouldn't be here, OR, this is inside a function so that items var is not global so the other code can't ready it.

It would be better if you can show the code before that to see the complete function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download