Categories
JavaScript Answers

How to fire event on file select with JavaScript?

Sometimes, we want to fire event on file select with JavaScript.

In this article, we’ll look at how to fire event on file select with JavaScript.

How to fire event on file select with JavaScript?

To fire event on file select with JavaScript, we can listen to the file input’s change event.

For instance, we write

<input type="file" id="file" name="file" />

to add a file input.

Then we write

document.getElementById("file").onchange = () => {
  // ...
};

to select the file input with getElementById.

And then we set its onchange property to a function that runs when file selection changes.

Conclusion

To fire event on file select with JavaScript, we can listen to the file input’s change event.

Categories
JavaScript Answers

How to convert timestamp to human date in JavaScript?

Sometimes, we want to convert timestamp to human date in JavaScript.

In this article, we’ll look at how to convert timestamp to human date in JavaScript.

How to convert timestamp to human date in JavaScript?

To convert timestamp to human date in JavaScript, we can use the date’s toDateString, toTimeString, or the toLocaleString methods.

For instance, we write

const d = new Date(1382086394000).toDateString();
const t = new Date(1382086394000).toTimeString();
const dt = new Date(1382086394000).toLocaleString();

to create a date object with the Date constructor.

Then we call toDateString to return a human readable date string.

We call toTimeString to return a human readable time string.

And we call toLocaleString to return a human readable localized date time string.

Conclusion

To convert timestamp to human date in JavaScript, we can use the date’s toDateString, toTimeString, or the toLocaleString methods.

Categories
JavaScript Answers

How to respond with a JSON object in Node.js?

Sometimes, we want to respond with a JSON object in Node.js.

In this article, we’ll look at how to respond with a JSON object in Node.js.

How to respond with a JSON object in Node.js?

To respond with a JSON object in Node.js, we call the Express res.json method.

For instance, we write

res.json({ foo: "bar" });

in a middleware or route handler function to return { foo: "bar" } as a JSON response.

Conclusion

To respond with a JSON object in Node.js, we call the Express res.json method.

Categories
JavaScript Answers

How to decode a string with escaped Unicode with JavaScript?

Sometimes, we want to decode a string with escaped Unicode with JavaScript.

In this article, we’ll look at how to decode a string with escaped Unicode with JavaScript.

How to decode a string with escaped Unicode with JavaScript?

To decode a string with escaped Unicode with JavaScript, we can use the decodeURIComponent function.

For instance, we write

const unescaped = decodeURIComponent(
  JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"')
);

to call decodeURIComponent with the string parsed with JSON.parse.

Then we the Unicode string will be unerscaped by decodeURIComponent.

Conclusion

To decode a string with escaped Unicode with JavaScript, we can use the decodeURIComponent function.

Categories
JavaScript Answers

How to get an object’s absolute position on the page in JavaScript?

Sometimes, we want to get an object’s absolute position on the page in JavaScript.

In this article, we’ll look at how to get an object’s absolute position on the page in JavaScript.

How to get an object’s absolute position on the page in JavaScript?

To get an object’s absolute position on the page in JavaScript, we can use the getBoundingClientRect method.

For instance, we write

const logo = document.getElementById("hlogo");
const logoTextRectangle = logo.getBoundingClientRect();

console.log(logoTextRectangle.left);
console.log(logoTextRectangle.right);

to select the element with getElementById.

Then we get an object with the position values with the getBoundingClientRect method.

Next, we get the left and right position values from the object returned by getBoundingClientRect.

Conclusion

To get an object’s absolute position on the page in JavaScript, we can use the getBoundingClientRect method.