Ewr Xcerq Ewr Xcerq - 2 months ago 14
jQuery Question

Selector for a list-jquery newb

I have a table like this


Here is my javascript to get an array of items

var iList = document.querySelectorAll('td:first');

and here is to get all mapped
td p

var iDesc = document.querySelectorAll('td:second p');

I would like to create a mapped then sort its content in the onload event by the way. Is what I am doing correct ? I mean the selector
and second with my example table

In the example above there is one
element containing 2


You should run a loop for each tr element and get the keys and values from td elements. Next, push them into an array.

var arr = new Array();
$('table tr').each(function() {
    var $row = $(this);
    var key = $row.find('td:first').html();
    var value = $row.find('td:last').html();
    arr.push([key, value]);

Finally, sort the array by it's first index.

arr.sort(function(a, b) {
    var valueA, valueB;
    valueA = a[0]; // Where 1 is your index, from your example
    valueB = b[0];
    if (valueA < valueB) {
        return -1;
    else if (valueA > valueB) {
        return 1;
    return 0;

Live Demo