Categories
JavaScript Answers

How to render HTML inside textarea?

Sometimes, we want to render HTML inside textarea.

In this article, we’ll look at how to render HTML inside textarea.

How to render HTML inside textarea?

To render HTML inside textarea, we replace it with a contenteditable div.

For instance, we write

<div contenteditable="true">
  This is the first line.<br />
  hello world
  <br /><span style="color: lightgreen">Great</span>.
</div>

to put some HTML inside the div.

We set its contenteditable attribute to true to make the div’s content editable.

Conclusion

To render HTML inside textarea, we replace it with a contenteditable div.

Categories
JavaScript Answers

How to create a JavaScript callback for knowing when an image is loaded?

Sometimes, we want to create a JavaScript callback for knowing when an image is loaded.

In this article, we’ll look at how to create a JavaScript callback for knowing when an image is loaded.

How to create a JavaScript callback for knowing when an image is loaded?

To create a JavaScript callback for knowing when an image is loaded, we can use the onload method.

For instance, we write

const logo = document.getElementById("sologo");

logo.onload = () => {
  console.log("The image has loaded!");
};

logo.src = "https://picsum.photos/200/300";

to get the img element with getElementById.

Then we set logo.onload to a function that runs when the image is loaded.

Finally, we set logo.src to the image URL to load the image.

Conclusion

To create a JavaScript callback for knowing when an image is loaded, we can use the onload method.

Categories
JavaScript Answers

How to get seconds since epoch in JavaScript?

Sometimes, we want to get seconds since epoch in JavaScript.

In this article, we’ll look at how to get seconds since epoch in JavaScript.

How to get seconds since epoch in JavaScript?

To get seconds since epoch in JavaScript, we get the timestamp of the datetime in milliseconds with getTime.

And then we divide that by 1000 to get the number of seconds since epoch.

For instance, we write

const d = new Date();
const seconds = d.getTime() / 1000;

to create the date d.

And then we call d.getTime to return the timestamp in milliseconds.

Then we divide that by 1000 to get the number of seconds since epoch.

Conclusion

To get seconds since epoch in JavaScript, we get the timestamp of the datetime in milliseconds with getTime.

And then we divide that by 1000 to get the number of seconds since epoch.

Categories
JavaScript Answers

How to subtract minutes from a date in JavaScript?

Sometimes, we want to subtract minutes from a date in JavaScript.

In this article, we’ll look at how to subtract minutes from a date in JavaScript.

How to subtract minutes from a date in JavaScript?

To subtract minutes from a date in JavaScript, we can subtract the timestamp in milliseconds from the current date.

For instance, we write

const MS_PER_MINUTE = 60000;
const myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);

to subtract the myEndDateTime timestamp in milliseconds by the durationInMinutes * MS_PER_MINUTE milliseconds, which is the durationInMinutes converted to milliseconds.

Then we pass that into the Date constructor to create a new date object.

Conclusion

To subtract minutes from a date in JavaScript, we can subtract the timestamp in milliseconds from the current date.

Categories
JavaScript Answers

How to append to an object with JavaScript?

Sometimes, we want to append to an object with JavaScript.

in this article, we’ll look at how to append to an object with JavaScript.

How to append to an object with JavaScript?

To append to an object with JavaScript, we can use the spread operator.

For instance, we write

let alerts = {
  1: { app: "helloworld", message: "message" },
  2: { app: "helloagain", message: "another message" },
};

alerts = {
  ...alerts,
  alertNo: 2,
};

to spread the existing properties of alerts into a new object with ....

And then we add the alertNo property to it.

Then we assign the new object back to alerts.

Conclusion

To append to an object with JavaScript, we can use the spread operator.