I am trying to create a matrix table. The values to be plugged into the matrix will be user input from an html table (basically a unique pairing table which pairs two elements at a time and for each pair a user can select which he prefers and by how much it is prefered on a scale of 1-9 using radio buttons). it is the preferences i.e. 1-9 that gets plugged into the matrix table and the number of unique pairing that detemines the lenght of the matrix.

The problem is now i want to get the values of clicked radios into the matix. I know how to get it but just don't know how to fit it into the matrix. Here is the code I have for now, pls ask me questions if needed thanks:

$(function(){
    var tableLenght = new Array();
    $('#matrix').click(function(){
    var counter = 0;
    $("#riskForm tr").each(function(){
        //ignores the header of the table (the title)
    if(counter >=1){
        tableLenght.push($(this).closest('tr').children("td:eq(1)").text());
    }
    counter++;
    });

// get unique attributes of in our table
var uniqueList = new Array();
//push first element onto list
uniqueList.push(tableLenght[0]); //pushes current elem onto the array
var cur = tableLenght[0]; //sets first element by default
for(var i = 1; i < tableLenght.length; i++){ //loops through the whole lenght of our array
    if(cur != tableLenght[i]){//checks if the current is not same as the next one
            cur = tableLenght[i]; //sets the new elemt
    uniqueList.push(tableLenght[i]); //pushes onto the array
    }
}
alert(uniqueList); //alerts only the unique table rows in the tableLenght array
var myArray = new Array(uniqueList);

    for (var i = 0; i < uniqueList; i++) {
        myArray[i] = new Array(uniqueList);
        for (var j = 0; j < uniqueList; j++) {
            myArray[i][j] = '';
        }
}
});

//to show/get the values of selected radio buttons
function showValues() {
    var fields = $( ":input").serializeArray();
    $( "#results" ).empty();
    jQuery.each( fields, function( i, field ) {
    $( "#results" ).append( field.value + " " );
    });
}

$( ":checkbox, :radio" ).click( showValues );
showValues();
$('#display').click(function (n) {
document.location.href="trialIndex.php"
    });
});

Your help is much appreciated!

Two points:

  • It is somewhat academic but javascript arrays are strictly unidimensional. There's no such thing as a multidimensional array in javascript, however you can create an array of arrays, which is almost the same thing. It appears that this is what you are trying to do here.

  • uniqueList is an array but you are trying to use it in several places as if it were a number. Consider uniqueList.length.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.