1

I have been trying to convert a JavaScript web form to Typescript, and have been unable to work out how to convert statements involving table rows, such as the following:

let body = document.getElementById("selectStackBody");

let rowCount = body.rows.length;

let row = body.insertRow(rowCount);

body.deleteRow(i);

I can see how to get the body by doing the following:

let body = <HTMLBodyElement>document.getElementById("selectStackBody");

but I can't see how to get the right syntax for the rows.

2
  • What's the problem? What errors are being thrown? What have you tried? Commented Mar 31, 2017 at 17:07
  • The HTMLBodyElement interface does not have a rows property. The HTMLTableSectionElement interface (which a tbody element uses), however, does. If you show the HTML on which you are attempting to run this code, it would be easier to help you. Commented Mar 31, 2017 at 17:13

1 Answer 1

1

You don't necessarily have to do anything TypeScript specific here. Basic DOM syntax can do this for you.

If document.getElementById("selectStackBody") gives to the table body node then simply do body.childNodes.length to get the number of rows.

Similarly, to append a row, use the DOM syntax to add a child node.

Here's a sample:

const body = document.getElementById('selectStackBody')

// Total number of rows
let numberOfRows = body.childNodes.length

// Add a new row
let newRow = document.createElement('tr')
body.appendChild(newRow)

// Remove the first row
body.removeChild(body.childNodes[0])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.