Categories
JavaScript Answers

How to check if an element with the given ID has focus with JavaScript?

Sometimes, we want to check if an element with the given ID has focus with JavaScript.

In this article, we’ll look at how to check if an element with the given ID has focus with JavaScript.

How to check if an element with the given ID has focus with JavaScript?

To check if an element with the given ID has focus with JavaScript, we can check if document.activeElement is the same element as the element with the given ID.

For instance, we write

const dummyEl = document.getElementById("myID");
const isFocused = document.activeElement === dummyEl;

to get the element with ID myID with getElementById.

Then we check if dummyEl is focused with

document.activeElement === dummyEl

Conclusion

To check if an element with the given ID has focus with JavaScript, we can check if document.activeElement is the same element as the element with the given ID.

Categories
JavaScript Answers

How to check if audio is playing with JavaScript?

Sometimes, we want to check if audio is playing with JavaScript.

In this article, we’ll look at how to check if audio is playing with JavaScript.

How to check if audio is playing with JavaScript?

To check if audio is playing with JavaScript, we can use the paused property of the audio element.

For instance, we write

const isPlaying = (audElem) => {
  return !audElem.paused;
};

to define the isPlaying function that returns !audElem.paused.

If audElem.paused is false, then the audio is playing.

audElem is the audio element.

Conclusion

To check if audio is playing with JavaScript, we can use the paused property of the audio element.

Categories
JavaScript Answers

How to capture response of form.submit with JavaScript?

Sometimes, we want to capture response of form.submit with JavaScript.

In this article, we’ll look at how to capture response of form.submit with JavaScript.

How to capture response of form.submit with JavaScript?

To capture response of form.submit with JavaScript, we can listen for the form’s submit event.

For instance, we write

<form id="myFormId" action="/api/process/form" method="post">
  ...
  <button type="submit">Submit</button>
</form>

to add a form with some fields.

Then we write

document.forms["myFormId"].addEventListener("submit", async (event) => {
  event.preventDefault();
  const resp = await fetch(event.target.action, {
    method: "POST",
    body: new URLSearchParams(new FormData(event.target)),
  });
  const body = await resp.json();
  console.log(body);
});

to get the form with ID myFormId with

document.forms["myFormId"]

And we call addEventListener on it with 'submit' and the form submit event callback to submit the form when the submit button is clicked.

In the event handler callback, we call fetch with the event.target.action, which is the value of the form’s action attribute, and an object that has the request method and body.

We get the form data from the form with

new FormData(event.target)

And convert it to a query string with URLSearchParams.

Finally we get the response from resp.json.

Conclusion

To capture response of form.submit with JavaScript, we can listen for the form’s submit event.

Categories
JavaScript Answers

How to fix ESLint not working in VS Code?

Sometimes, we want to fix ESLint not working in VS Code.

In this article, we’ll look at how to fix ESLint not working in VS Code.

How to fix ESLint not working in VS Code?

To fix ESLint not working in VS Code, we add some options to settings.json.

For instance, we add

{
  //...
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "html",
    "typescriptreact"
  ]
  //...
}

in settings.json to enable validation with ESLint in VS Code.

We add this option to make ESLint validate JavaScript, JSX, HTML, and TypeScript React files.

Conclusion

To fix ESLint not working in VS Code, we add some options to settings.json.

Categories
JavaScript Answers

How to get the day of week and the month of the year with JavaScript?

Sometimes, we want to get the day of week and the month of the year with JavaScript.

In this article, we’ll look at how to get the day of week and the month of the year with JavaScript.

How to get the day of week and the month of the year with JavaScript?

To get the day of week and the month of the year with JavaScript, we can use the date’s toLocaleTimeString method.

For instance, we write

const options = {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false,
};
const prnDt = new Date().toLocaleTimeString("en-us", options);

to call toLocaleDateString on the Date object with the current date time with the locale string and options.

In options, we specify how to format different parts of the dates.

And it returns a date string with the formatted date and time.

'numeric' means the full number, '2-digit' formats the number as a 2 digit number.

'long' returns the full human readable date or time part.

Conclusion

To get the day of week and the month of the year with JavaScript, we can use the date’s toLocaleTimeString method.