Categories
JavaScript Answers

How to move element in the DOM in JavaScript?

Sometimes, we want to move element in the DOM in JavaScript.

In this article, we’ll look at how to move element in the DOM in JavaScript.

How to move element in the DOM in JavaScript?

To move element in the DOM in JavaScript, we can use the before and after methods.

For instance, we write

const div1 = document.getElementById("div1");
const div2 = document.getElementById("div2");
const div3 = document.getElementById("div3");

div2.after(div1);
div2.before(div3);

to select 3 divs with getElementById.

Then we call div2.after with div1 to insert div1 after div2.

And we call div2.before with div3 to insert div3 before div2.

Conclusion

To move element in the DOM in JavaScript, we can use the before and after methods.

Categories
JavaScript Answers

How to query Firestore database for document ID with JavaScript?

Sometimes, we want to query Firestore database for document ID with JavaScript.

In this article, we’ll look at how to query Firestore database for document ID with JavaScript.

How to query Firestore database for document ID with JavaScript?

To query Firestore database for document ID with JavaScript, we call the doc method.

For instance, we write

const book = db.collection("books").doc("fK3ddutEpD2qQqRMXNW5").get();

to get the books collection with

db.collection("books")

Then we call doc to get the entry from the books collection with ID 'fK3ddutEpD2qQqRMXNW5'.

Finally, we call get to return the object with the given ID.

Conclusion

To query Firestore database for document ID with JavaScript, we call the doc method.

Categories
JavaScript Answers

How to set img src with JavaScript?

Sometimes, we want to set img src with JavaScript.

In this article, we’ll look at how to set img src with JavaScript.

How to set img src with JavaScript?

To set img src with JavaScript, we set the src property of the img element.

For instance, we write

const image = document.createElement("img");
image.src = "https://picsum.photos/200/300";

to call createElement to create an img element.

Then we set its src attribute by setting the src property to the image URL.

Conclusion

To set img src with JavaScript, we set the src property of the img element.

Categories
JavaScript Answers

How to fix UTC gives wrong date with moment.js and JavaScript?

To fix UTC gives wrong date with moment.js and JavaScript, we should add the time zone to the datetime string we’re parsing into a moment object.

For instance, we write

const datetime = moment(new Date("07-18-2022 UTC"))
  .utc()
  .format("YYYY-MM-DD HH:mm");

to call moment with the new Date("07-18-2022 UTC") date, which we specified to be UTC.

Then we call utc to return a UTC moment.

Next, we call format to return a datetime string in specified format.

Categories
JavaScript Answers

How to use await outside of an async function with JavaScript?

Sometimes, we want to use await outside of an async function with JavaScript.

In this article, we’ll look at how to use await outside of an async function with JavaScript.

How to use await outside of an async function with JavaScript?

To use await outside of an async function with JavaScript, we can call the async function right after we defined it.

For instance, we write

(async () => {
  await foo();
  // ...
})();

to define an async function.

Then we wrap it in parentheses and add () at the end to call it immediately after it’s defined.

Conclusion

To use await outside of an async function with JavaScript, we can call the async function right after we defined it.