Categories
JavaScript Answers

How to iterate through a table rows and get the cell values using JavaScript?

Sometimes, we want to iterate through a table rows and get the cell values using JavaScript.

In this article, we’ll look at how to iterate through a table rows and get the cell values using JavaScript.

How to iterate through a table rows and get the cell values using JavaScript?

To iterate through a table rows and get the cell values using JavaScript, we can use the row property.

For instance, we write:

<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
    </tr>
    <tr>
      <td>B1</td>
      <td>B2</td>
      <td>B3</td>
    </tr>
    <tr>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

const table = document.querySelector('table')
for (const r of table.rows) {
  for (const c of r.cells) {
    console.log(c.innerHTML)
  }
}

to select the table with querySelector.

Then we loop through the table.rows property which is an object with the tr elements.

Then we loop through the cells which has the th and td elements.

And then we get the innerHTML value to get its contents.

Conclusion

To iterate through a table rows and get the cell values using JavaScript, we can use the row property.

Categories
JavaScript Answers React React Answers

How to access canvas context in React and JavaScript?

Sometimes, we want to access canvas context in React and JavaScript.

In this article, we’ll look at how to access canvas context in React and JavaScript.

How to access canvas context in React and JavaScript?

To access canvas context in React and JavaScript, we can assign a ref to the canvas and call getContext on the ref’s value.

For instance, we write:

import React from "react";

export default function App() {
  const ref = React.useRef();
  console.log(ref.current.getContext("2d"));

  return (
    <>
      <canvas ref={ref} />
    </>
  );
}

to call useRef to create a ref.

Then we get the context by calling ref.current.getContext with '2d' to get the context.

Conclusion

To access canvas context in React and JavaScript, we can assign a ref to the canvas and call getContext on the ref’s value.

Categories
JavaScript Answers

How to add a custom upload button with JavaScript?

Sometimes, we want to add a custom upload button with JavaScript.

In this article, we’ll look at how to add a custom upload button with JavaScript.

How to add a custom upload button with JavaScript?

To add a custom upload button with JavaScript, we can add a hidden file input and then call click on it to open it.

For instance, we write:

<input type='file' style='display: none'>
<button>
  open
</button>

to add a file input and a button.

Then we write:

const input = document.querySelector('input');
const button = document.querySelector('button');
button.onclick = () => {
  input.click()
}

to select the input and button with querySelector.

Then we set button.onclick to a function that calls input.click to open the file selection dialog.

Conclusion

To add a custom upload button with JavaScript, we can add a hidden file input and then call click on it to open it.

Categories
JavaScript Answers

How to remove illegal characters from a file name but leave spaces with JavaScript?

Sometimes, we want to remove illegal characters from a file name but leave spaces with JavaScript.

In this article, we’ll look at how to remove illegal characters from a file name but leave spaces with JavaScript.

How to remove illegal characters from a file name but leave spaces with JavaScript?

To remove illegal characters from a file name but leave spaces with JavaScript, we can use the string replace method.

For instance, we write:

const filename = "f?:i/le>  n%a|m\\e.ext";
const fixedFilename = filename.replace(/[/\\?%*:|"<>]/g, '');
console.log(fixedFilename);

to call filename.replace with a regex that matches all instances of all the characters we want to remove.

Then we remove them by replacing them with empty strings.

Therefore, fixedFilename is 'file name.ext'.

Conclusion

To remove illegal characters from a file name but leave spaces with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to add white space to left of a string with JavaScript?

Sometimes, we want to add white space to left of a string with JavaScript.

In this article, we’ll look at how to add white space to left of a string with JavaScript.

How to add white space to left of a string with JavaScript?

To add white space to left of a string with JavaScript, we can use the string padStart method.

For instance, we write:

const str = 'blah'
const paddedStr = str.padStart(10, ' ')
console.log(paddedStr)

to call str.padStart with 10 and ' ' to pad the string to length 10 by prepending spaces to the string.

Therefore, paddedStr is ' blah'.

Conclusion

To add white space to left of a string with JavaScript, we can use the string padStart method.