Skip to content
Advertisement

get values from an html table

I need to write a website that calculates the inverse matrix. I have a table. I also wrote a function to resize the table. I need the entered values to be read into a two-dimensional array (arr), and I can use this array as a function argument (PrintMatrix(arr)). I’m new to this and I’m not very good at it

My table:

JavaScript

function for resizing:

JavaScript

Also, i have it, but it only works for a 3×3 table and if you change the size of the table, you still get a 3×3 array. Also, for some reason, the array values are “undefined”

JavaScript

Advertisement

Answer

If you want a function that returns a matrix with the input of your user, based on your code, you can use a more simple approach using the ids that you’ve set, with something like this:

JavaScript

For what concern your attempt to create this function, you get only undefined values because you are attempting to get the value property on a HTMLCollection object; indeed the children read-only property returns, as you can see from the following quote from the docs, a live collection of children elements.

The read-only children property returns a live HTMLCollection which contains all of the child elements of the element upon which it was called.

An HTMLCollection which is a live, ordered collection of the DOM elements which are children of node. You can access the individual child nodes in the collection by using either the item() method on the collection, or by using JavaScript array-style notation.

If the element has no element children, then children is an empty list with a length of 0.

To obtain the desired behavior, in this case, you could just access the first element of the collection, that is tableArray[i][j] = (tr[i].children[j].children[0].value);.

Also, when resizing the matrix, you forgot to insert a tbody tag, so your function will always fire an exception when the size of the matrix has been changed.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement