Categories
JavaScript Answers

How to focus input box on load with JavaScript?

Sometimes, we want to focus input box on load with JavaScript.

In this article, we’ll look at how to focus input box on load with JavaScript.

How to focus input box on load with JavaScript?

To focus input box on load with JavaScript, we can call the input element’s focus method.

For instance, we write

<input id="myinputbox" type="text" />

to add an input element.

Then we write

window.onload = () => {
  document.getElementById("myinputbox").focus();
};

to select the input with getElementById.

Then we call focus on it to focus it.

We put the code in window.onload to run it when the input is loaded.

Conclusion

To focus input box on load with JavaScript, we can call the input element’s focus method.

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.