Categories
JavaScript Answers

How to set caret position at a specific position in contenteditable div with JavaScript?

Sometimes, we want to set caret position at a specific position in contenteditable div with JavaScript.

In this article, we’ll look at how to set caret position at a specific position in contenteditable div with JavaScript.

How to set caret position at a specific position in contenteditable div with JavaScript?

To set caret position at a specific position in contenteditable div with JavaScript, we select the text node that with the text we want the cursor to be at and set the caret position there.

For instance, we write:

<div contenteditable>
  hello world
</div>

to add a contenteditable div.

Then we write:

const node = document.querySelector("div");
node.focus();
const textNode = node.firstChild;
const caret = 10; 
const range = document.createRange();
range.setStart(textNode, caret);
range.setEnd(textNode, caret);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

to select the div with querySelector.

We call focus to focus on the div.

Then we get the text node in the div with node.firstChild.

Next, we call document.createRange to create the selection range.

And then we call setStart and setEnd to set the select range.

Next, we call window.getSelection to get the selection.

And we call removeAllRanges to remove all selection.

And then we call addRange with range to move the cursor.

Conclusion

To set caret position at a specific position in contenteditable div with JavaScript, we select the text node that with the text we want the cursor to be at and set the caret position there.

Categories
JavaScript Answers

How to toggle audio play pause with a button or link with JavaScript?

Sometimes, we want to toggle audio play pause with a button or link with JavaScript.

In this article, we’ll look at how to toggle audio play pause with a button or link with JavaScript.

How to toggle audio play pause with a button or link with JavaScript?

To toggle audio play pause with a button or link with JavaScript, we can use a flag to keep track of when the audio is playing or paused.

For instance, we write:

<audio controls src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"></audio>
<button>toggle</button>

to add an audio and button element.

Then we write:

const audio = document.querySelector('audio')
const button = document.querySelector('button')

let isPlaying = false;

button.onclick = () => {
  isPlaying ? audio.pause() : audio.play();
};
audio.onplaying = () => {
  isPlaying = true;
};
audio.onpause = () => {
  isPlaying = false;
};

to select the audio and button elements with querySelector.

Then we set button.onclick to a click event handler function that checks if isPlaying is true or false.

If it’s true, then we pause the audio with audio.pause.

Otherwise, we play the audio with audio.play.

Then we set audio.onplaying and audio.onpause to functions that sets isPlaying to true and false respectively.

Conclusion

To toggle audio play pause with a button or link with JavaScript, we can use a flag to keep track of when the audio is playing or paused.

Categories
JavaScript Answers

How to get a UTC Date object in Node.js and JavaScript?

Sometimes, we want to get a UTC Date object in Node.js and JavaScript.

In this article, we’ll look at how to get a UTC Date object in Node.js and JavaScript.

How to get a UTC Date object in Node.js and JavaScript?

To get a UTC Date object in Node.js and JavaScript, we can call the date’s toUTCString method.

For instance, we write:

console.log(new Date().toUTCString())

to create a new Date instance and call toUTCString to return the UTC date string version of the current date time.

Conclusion

To get a UTC Date object in Node.js and JavaScript, we can call the date’s toUTCString method.

Categories
JavaScript Answers

How to check if server is online with JavaScript?

Sometimes, we want to check if server is online with JavaScript.

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

How to check if server is online with JavaScript?

To check if server is online with JavaScript, we can use the AbortController to cancel the request when the request times out.

For instance, we write:

(async () => {
  const url = 'https://example.com'
  const controller = new AbortController();
  const signal = controller.signal;
  const options = {
    mode: 'no-cors',
    signal
  };
  await fetch(url, options)

  setTimeout(() => {
    controller.abort()
  }, 3000)
})()

to call fetch with a url and options.

options has the signal property assigned to the AbortController instance’s signal property.

Then we call controller.abort to cancel the request to url after 3000 milliseconds by calling it in the setTimeout callback.

If the request is done before 3 seconds, then abort will do nothing.

Conclusion

To check if server is online with JavaScript, we can use the AbortController to cancel the request when the request times out.

Categories
JavaScript Answers

How to get a date range using moment.js and JavaScript?

Sometimes, we want to get a date range using moment.js and JavaScript.

In this article, we’ll look at how to get a date range using moment.js and JavaScript.

How to get a date range using moment.js and JavaScript?

To get a date range using moment.js and JavaScript, we can use the moment add method.

For instance, we write:

const startDate = moment('2022-01-01')
const endDate = moment('2022-01-21')
let d = startDate
const range = []
while (+d.toDate() < +endDate.toDate()) {
  range.push(d.toDate())
  d = d.add(1, 'days')
}
console.log(range)

to set the start and end date of the range to startDate and endDate.

Then we set d to startDate.

Next, we use a while loop that runs until d is later than endDate.

In the loop, we call range.push with d.toDate() to push the moment object d converted to a date.

Then we call add with 1 and 'days' to add one day.

Therefore, range is has the dates from Jan 1, 2022 to Jan 20, 2022.

Conclusion

To get a date range using moment.js and JavaScript, we can use the moment add method.