Categories
JavaScript Answers

How to change cursor to waiting in JavaScript?

Sometimes, we want to change cursor to waiting in JavaScript.

In this article, we’ll look at how to change cursor to waiting in JavaScript.

How to change cursor to waiting in JavaScript?

To change cursor to waiting in JavaScript, we can set the cursor style.

For instance, we write

document.body.style.cursor = 'wait'

to change the cursor style to spinner.

And we set the cursor style back to the normal style with

document.body.style.cursor = 'default'

We set the style on the body element with both lines so we see the cursor no matter where the mouse is on the page.

Conclusion

To change cursor to waiting in JavaScript, we can set the cursor style.

Categories
JavaScript Answers

How to perform a str_replace in JavaScript, replacing text in JavaScript?

Sometimes, we want to perform a str_replace in JavaScript, replacing text in JavaScript.

In this article, we’ll look at how to perform a str_replace in JavaScript, replacing text in JavaScript.

How to perform a str_replace in JavaScript, replacing text in JavaScript?

To perform a str_replace in JavaScript, replacing text in JavaScript, we use the string replace method.

For instance, we write

text = text.replace("old", "new");

to replace the first instance of substring 'old' with 'new' in the text string.

We can also call replace with a regex as the first argument to match substring patterns in the string and do the replacemennt.

Conclusion

To perform a str_replace in JavaScript, replacing text in JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to parse a string with a comma thousand separator to a number with JavaScript?

Sometimes, we want to parse a string with a comma thousand separator to a number with JavaScript.

In this article, we’ll look at how to parse a string with a comma thousand separator to a number with JavaScript.

How to parse a string with a comma thousand separator to a number with JavaScript?

To parse a string with a comma thousand separator to a number with JavaScript, we remove all the commas from the string before we parse it.

For instance, we write

const output = parseFloat("2,299.00".replace(/,/g, ""));
console.log(output);

to return the "2,299.00" string without the commas with replace.

Then we call parseFloat to parse the returned string into a floating point number.

Conclusion

To parse a string with a comma thousand separator to a number with JavaScript, we remove all the commas from the string before we parse it.

Categories
JavaScript Answers

How to validate date with format mm/dd/yyyy in JavaScript?

Sometimes, we want to validate date with format mm/dd/yyyy in JavaScript.

In this article, we’ll look at how to validate date with format mm/dd/yyyy in JavaScript.

How to validate date with format mm/dd/yyyy in JavaScript?

To validate date with format mm/dd/yyyy in JavaScript, we can use moment.js.

For instance, we write

console.log(moment("05/22/2022", "MM/DD/YYYY", true).isValid());

to check that "05/22/2022" matches the "MM/DD/YYYY" format with moment.

We call moment with true to make sure the date string matches the date format in the 2nd argument exactly.

Then we call isValid to return whether the date string is valid in the given format or not.

Conclusion

To validate date with format mm/dd/yyyy in JavaScript, we can use moment.js.

Categories
JavaScript Answers

How to record audio to file with JavaScript?

Sometimes, we want to record audio to file with JavaScript.

In this article, we’ll look at how to record audio to file with JavaScript.

How to record audio to file with JavaScript?

To record audio to file with JavaScript, we use the MediaRecorder API.

For instance, we write

let audioChunks = [];
let rec;

navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
  handlerFunction(stream);
});

const sendData = (blob) => {
  //...
};
const handlerFunction = (stream) => {
  rec = new MediaRecorder(stream);
  rec.ondataavailable = (e) => {
    audioChunks.push(e.data);
    if (rec.state == "inactive") {
      let blob = new Blob(audioChunks, { type: "audio/mp3" });
      recordedAudio.src = URL.createObjectURL(blob);
      recordedAudio.controls = true;
      recordedAudio.autoplay = true;
      sendData(blob);
    }
  };
};

record.onclick = (e) => {
  record.disabled = true;
  record.style.backgroundColor = "blue";
  stopRecord.disabled = false;
  audioChunks = [];
  rec.start();
};

stopRecord.onclick = (e) => {
  record.disabled = false;
  stop.disabled = true;
  record.style.backgroundColor = "red";
  rec.stop();
};

to check if we have audio permission with

navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
  handlerFunction(stream);
});

If we have then we call handlerFunction to handle the stream. We record the audio by calling rec.start, where rec is a MediaRecorder instance we created with the stream.

And in the stopRecord.onclick method, we call rec.stop to stop recording.

Then if we check that rec.state is 'inactive', we create an mp3 blob from the recorded audio with

let blob = new Blob(audioChunks,{type:'audio/mp3'});

Then we can set URL.createObjectURL(blob); as the src property of the recordedAudio audio element so we can play the recorded audio.

Conclusion

To record audio to file with JavaScript, we use the MediaRecorder API.