Categories
JavaScript Answers

How to truncate decimal numbers in JavaScript?

Sometimes, we want to truncate decimal numbers in JavaScript.

In this article, we’ll look at how to truncate decimal numbers in JavaScript.

How to truncate decimal numbers in JavaScript?

To truncate decimal numbers in JavaScript, we can use the Math.trunc method.

For instance, we write

const a = 5.467;
const truncated = Math.trunc(a * 100) / 100;

to call Math.trunc with the number a multiplied by 100 to truncate the number.

Then we divide the truncated number by 100 to return a truncated to 2 decimal places.

Conclusion

To truncate decimal numbers in JavaScript, we can use the Math.trunc method.

Categories
JavaScript Answers

How to convert seconds to HH:mm:ss in moment.js and JavaScript?

Sometimes, we want to convert seconds to HH:mm:ss in moment.js and JavaScript.

In this article, we’ll look at how to convert seconds to HH:mm:ss in moment.js and JavaScript.

How to convert seconds to HH:mm:ss in moment.js and JavaScript?

To convert seconds to HH:mm:ss in moment.js and JavaScript, we can use the format method.

For instance, we write

const secs = 456;

const formatted = moment.utc(secs * 1000).format("HH:mm:ss");

console.log(formatted);

to call moment.utc with the sec seconds converted to milliseconds to create a moment object with the milliseconds.

Then we call format with "HH:mm:ss" to convert the milliseconds to a string in HH:mm:ss format.

Conclusion

To convert seconds to HH:mm:ss in moment.js and JavaScript, we can use the format method.

Categories
JavaScript Answers

How to get image dimensions with JavaScript?

Sometimes, we want to get image dimensions with JavaScript.

In this article, we’ll look at how to get image dimensions with JavaScript.

How to get image dimensions with JavaScript?

To get image dimensions with JavaScript, we can use the width and height properties.

For instance, we write

const img = new Image();

img.onload = () => {
  const height = img.height;
  const width = img.width;
  //...
};

img.src = url;

to create an img element with the Image constructor.

Then we get the height and width of the img image in the img.onload method, which runs when the image is loaded.

Then we load the image by setting img.src to the url of the image.

Conclusion

To get image dimensions with JavaScript, we can use the width and height properties.

Categories
JavaScript Answers

How to add a checkbox check event listener with JavaScript?

Sometimes, we want to add a checkbox check event listener with JavaScript.

In this article, we’ll look at how to add a checkbox check event listener with JavaScript.

How to add a checkbox check event listener with JavaScript?

To add a checkbox check event listener with JavaScript, we can listen for the change event.

For instance, we write

<input type="checkbox" name="checkbox" />

to add a checkbox.

Then we write

const checkbox = document.querySelector("input[name=checkbox]");

checkbox.addEventListener("change", (e) => {
  if (e.target.checked) {
    console.log("Checkbox is checked..");
  } else {
    console.log("Checkbox is not checked..");
  }
});

to select the checkbox with querySelector.

Then we call checkbox.addEventListener to add an event listener for the change event.

We get the checked value of the checkbox with e.target.checked.

If it’s true, it’s checked.

Otherwise, it’s not.

Conclusion

To add a checkbox check event listener with JavaScript, we can listen for the change event.

Categories
JavaScript Answers

How to copy or put text on the clipboard with JavaScript?

Sometimes, we want to copy or put text on the clipboard with JavaScript.

In this article, we’ll look at how to copy or put text on the clipboard with JavaScript.

How to copy or put text on the clipboard with JavaScript?

To copy or put text on the clipboard with JavaScript, we can use the document.execCommand method.

For instance, we write

const copy = () => {
  document.getElementById("myText").select();
  document.execCommand("copy");
};

to create copy function that selects the input element with ID myText with getElementById.

Then we call select on it to select the input text.

Next we call document.execCommand with 'copy' to copy the selected text into the clipboard.

Conclusion

To copy or put text on the clipboard with JavaScript, we can use the document.execCommand method.