Categories
JavaScript Answers

How to Add an Click Event Handler to a Table Row with JavaScript?

Sometimes, we want to add an click event handler to a table row with JavaScript.

In this article, we’ll look at how to add an click event handler to a table row with JavaScript.

Add an Click Event Handler to a Table Row with JavaScript

To add an click event handler to a table row with JavaScript, we can loop through each row and set the onclick property of each row to an event handler.

For instance, we add a table by writing:

<table>
  <tbody>
    <tr>
      <td>foo</td>
      <td>bar</td>
    </tr>
    <tr>
      <td>baz</td>
      <td>qux</td>
    </tr>
  </tbody>
</table>

Then we write:

const createClickHandler = (row) => {
  return () => {
    const [cell] = row.getElementsByTagName("td");
    const id = cell.innerHTML;
    console.log(id);
  };
};

const table = document.querySelector("table");
for (const currentRow of table.rows) {
  currentRow.onclick = createClickHandler(currentRow);
}

to add event handlers toe each row.

We define the createClickHandler function that takes the table row as a parameter.

And we get the first td in the row with getElementsByTagName and destructuring.

Then we get the cell‘s innerHTML property and log that in the handler.

Next, we select the table element with document.querySelector.

And then we use the for-of loop to loop through each table row which we get with table.rows.

And then we assign the onClick property of each row to the event handler created by createClickHandler.

Conclusion

To add an click event handler to a table row with JavaScript, we can loop through each row and set the onclick property of each row to an event handler.

Categories
JavaScript Answers

How to Get the Selected File’s Size in JavaScript?

Sometimes, we want to get the selected file’s size in JavaScript.

In this article, we’ll look at how to get the selected file’s size in JavaScript.

Get the Selected File’s Size in JavaScript

To get the selected file’s size in JavaScript, we can use the size property of the file object to get the file’s size in bytes.

For instance, we write:

<input type='file'>

to add a file input.

Then, we write:

const fileInput = document.querySelector("input");
fileInput.addEventListener('change', (e) => {
  const [file] = e.target.files
  console.log(file.size)
})

to select the file input with document.querySelector.

Then we call addEventListener with 'change' to listen for file selection.

In the event handler callback, we get the file from e.target.files with destructuring.

Then we get size of the file in bytes with the size property.

Now we should see the selected file’s size logged whenever we select a file.

Conclusion

To get the selected file’s size in JavaScript, we can use the size property of the file object to get the file’s size in bytes.

Categories
JavaScript Answers

How to Check Whether an Array Exists in an Array of Arrays with JavaScript?

Sometimes, we want to check whether an array exists in an array of arrays with JavaScript.

In this article, we’ll look at how to check whether an array exists in an array of arrays with JavaScript.

Check Whether an Array Exists in an Array of Arrays with JavaScript

To check whether an array exists in an array of arrays with JavaScript, we can stringify both arrays with the JSON.stringify method.

Then we can use the JavaScript string indexOf method to check whether an array exists in an array of arrays by checking the stringified versions of them.

For instance, we write:

const a = [
  [1, 2],
  [3, 4]
];
const b = [1, 2];
const aStr = JSON.stringify(a);
const bStr = JSON.stringify(b);
const exist = aStr.indexOf(bStr) !== -1;
console.log(exist)

to define and a and b arrays.

Then we call JSON.stringify with a and b to return the string versions of the array.

Next, we call aStr.indexOf with bstr to check if bStr is in aStr.

And if indexOf returns anything other than -1, then we know bStr is in aStr.

Therefore, exist is true according to the console log.

Conclusion

To check whether an array exists in an array of arrays with JavaScript, we can stringify both arrays with the JSON.stringify method.

Then we can use the JavaScript string indexOf method to check whether an array exists in an array of arrays by checking the stringified versions of them.

Categories
JavaScript Answers

How to Get Intersection of Keys of Two Objects with JavaScript?

Sometimes, we want to get intersection of keys of two objects with JavaScript.

In this article, we’ll look at how to get intersection of keys of two objects with JavaScript.

Get Intersection of Keys of Two Objects with JavaScript

To get intersection of keys of two objects with JavaScript, we can use the Object.keys method to get the keys of an object.

Then we can use the JavaScript array filter method to get an array of keys that exist in both objects.

For instance, we write:

const a = {
  x: undefined,
  y: 1,
  z: 2,
  a: 10,
  b: 20,
  e: 30
}
const b = {
  x: 0,
  y: 1,
  z: 2,
  a: 10,
  c: 20,
  d: 30
}

const intersect = (o1, o2) => {
  return Object.keys(o1).filter(k => k in o2)
}

console.log(intersect(a, b))

We have 2 objects a and b that have some overlapping keys.

Then we create the intersect function that takes 2 objects o1 and o2.

And we call Object.keys with o1 to return an array of key name strings in o1.

Then we call filter with a callback that checks if key k is on o2 with the in operator.

Therefore, from the console log, we see that the intersection of keys between o1 and o2 is ['x', 'y', 'z', 'a'].

Conclusion

To get intersection of keys of two objects with JavaScript, we can use the Object.keys method to get the keys of an object.

Then we can use the JavaScript array filter method to get an array of keys that exist in both objects.

Categories
JavaScript Answers

How to Get the Decimal Places of a Floating Point Number in JavaScript?

Sometimes, we want to get the decimal places of a floating point number in JavaScript.

In this article, we’ll look at how to get the decimal places of a floating point number in JavaScript.

Get the Decimal Places of a Floating Point Number in JavaScript

To get the decimal places of a floating point number in JavaScript, we can convert it to a string and split it by the decimal point.

Then we can get the number of characters after the point.

For instance, we write:

const [, decimal] = (12.3456).toString().split(".")
const precision = decimal.length;
console.log(precision)

to call toString on 12.3456 to convert it to a string.

Then we call split with '.' on the string to split it by the decimal place.

Next, we assign the 2nd entry from the split string array to decimal.

And the we get the length of decimal and assign it to precision.

Therefore, precision is 4 according to the console log.

Conclusion

To get the decimal places of a floating point number in JavaScript, we can convert it to a string and split it by the decimal point.

Then we can get the number of characters after the point.