Categories
JavaScript Answers

How to a remove portion of string in JavaScript?

Sometimes, we want to a remove portion of string in JavaScript.

In this article, we’ll look at how to a remove portion of string in JavaScript.

How to a remove portion of string in JavaScript?

To a remove portion of string in JavaScript, we can call the string replace method with the substring we want to remove and replace it with an empty string.

For instance, we write:

const string = '@usernameWhats on your mind?'
const newString = string.replace('Whats on your mind?', '');
console.log(newString)

We call string.replace to replace 'Whats on your mind?' with an empty string and return the new string.

Therefore, newString is '@username'.

Conclusion

To a remove portion of string in JavaScript, we can call the string replace method with the substring we want to remove and replace it with an empty string.

Categories
JavaScript Answers

How to create label and check box dynamically in JavaScript?

Sometimes, we want to create label and check box dynamically in JavaScript.

In this article, we’ll look at how to create label and check box dynamically in JavaScript.

How to create label and check box dynamically in JavaScript?

To create label and check box dynamically in JavaScript, we can use the createElement method.

For instance, we write:

const newLabel = document.createElement("label");
newLabel.setAttribute("for", 'checkbox');
newLabel.innerHTML = "Here goes the text";

const newCheckbox = document.createElement("input");
newCheckbox.setAttribute("type", 'checkbox');
newCheckbox.setAttribute("id", 'checkbox');

document.body.appendChild(newLabel);
document.body.appendChild(newCheckbox);

We call document.createElement to create a label.

Then we call setAttribute to set the for attribute to checkbox.

And we set the innerHTML property to add some text to the label.

Next, we call createElement again to create an input.

Next, we call setAttribute to set the type attribute to checkbox to make it a checkbox.

We call the same method to set the id attribute.

Finally, we call document.body.appendChild to append them both to the body element.

Conclusion

To create label and check box dynamically in JavaScript, we can use the createElement method.

Categories
JavaScript Answers Nodejs

How to add to an existing JSON file in Node.js?

Sometimes, we want to add to an existing JSON file in Node.js.

In this article, we’ll look at how to add to an existing JSON file in Node.js.

How to add to an existing JSON file in Node.js?

To add to an existing JSON file in Node.js, we can use the fs.readFile method.

For instance, we have:

["foo","bar"]

in results.json

Then we write:

const { promises: fs } = require("fs");
const currentSearchResult = 'example'

const write = async () => {
  const data = await fs.readFile('results.json')
  const json = JSON.parse(data)
  json.push(currentSearchResult)
  await fs.writeFile("results.json", JSON.stringify(json))
}
write()

to call fs.readFile to read results.json into data.

Then we call JSON.parse with data to parse data into an onject.

Then we call json.push with currentSearchResult to add currentSearchResult to json assuming json is an array.

Finally, we call fs.writeFile with the file path and the content to write to the file.

Now result.json has:

["foo","bar","example"]

Conclusion

To add to an existing JSON file in Node.js, we can use the fs.readFile method.

Categories
JavaScript Answers

How to convert a JavaScript timestamp into UTC format?

Sometimes, we want to convert a JavaScript timestamp into UTC format.

In this article, we’ll look at how to convert a JavaScript timestamp into UTC format.

How to convert a JavaScript timestamp into UTC format?

To convert a JavaScript timestamp into UTC format, we can call the toUTCString method.

For instance, we write:

const s = (new Date(1291656749000)).toUTCString()
console.log(s)

We create a date with a timestamp.

Then we call toUTCString to return the UTC date string from the date.

As a result, s is 'Mon, 06 Dec 2010 17:32:29 GMT'.

Conclusion

To convert a JavaScript timestamp into UTC format, we can call the toUTCString method.

Categories
JavaScript Answers

How to mute all sound in a page with JavaScript?

Sometimes, we want to mute all sound in a page with JavaScript.

In this article, we’ll look at how to mute all sound in a page with JavaScript.

How to mute all sound in a page with JavaScript?

To mute all sound in a page with JavaScript, we can set the muted property of each audio element to true.

For instance, we write:

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

to add the audio elements.

Then we write:

const elems = document.querySelectorAll("video, audio");
for (const el of elems) {
  el.muted = true
  el.pause()
}

We select all the audio and video elements with querySelectorAll.

Then we loop through each element in elems and set the muted property of it to true to mute the audio.

We can also call pause to pause the audio.

Conclusion

To mute all sound in a page with JavaScript, we can set the muted property of each audio element to true.