Categories
JavaScript Answers

How to add an element to the DOM with JavaScript?

Sometimes, we want to add an element to the DOM with JavaScript.

In this article, we’ll look at how to add an element to the DOM with JavaScript.

How to add an element to the DOM with JavaScript?

To add an element to the DOM with JavaScript, we can use the appendChild method.

For instance, we write:

const newDiv = document.createElement("div");
newDiv.appendChild(document.createTextNode("some text"));
document.body.appendChild(newDiv);

to create a new div with createElement.

Then we call createTextNode to create a new text node.

And then we append the text node as the child of the div with newDiv.appendChild.

Finally, we append the div as a child of the body element with document.body.appendChild.

Conclusion

To add an element to the DOM with JavaScript, we can use the appendChild method.

Categories
JavaScript Answers

How to detect middle mouse button click with JavaScript?

Sometimes, we want to detect middle mouse button click with JavaScript.

In this article, we’ll look at how to detect middle mouse button click with JavaScript.

How to detect middle mouse button click with JavaScript?

To detect middle mouse button click with JavaScript, we can check which button is clicked with e.button in the mousedown event handler.

For instance, we write:

window.onmousedown = (e) => {
  if (e.button === 1) {
    console.log('middle clicked')
  }
}

to set window.onmousedown to a function that checks if the middle mouse button is clicked by checking if e.button is 1.

If it is clicked, then 'middle clicked' is logged.

Conclusion

To detect middle mouse button click with JavaScript, we can check which button is clicked with e.button in the mousedown event handler.

Categories
JavaScript Answers

How to change time with moment.js and JavaScript?

Sometimes, we want to change time with moment.js and JavaScript.

In this article, we’ll look at how to change time with moment.js and JavaScript.

How to change time with moment.js and JavaScript?

To change time with moment.js and JavaScript, we can use the set method.

For instance, we write:

const m = moment('2022-01-01', 'YYYY-MM-DD')
m.set({
  h: 11,
  m: 11
});
console.log(m)

to create a moment object from a date string with moment.

Then we call set on the returned moment object with an object that sets the hour h and minute m.

Therefore, the new value of the moment object is Jan 01 2022 11:11:00.

Conclusion

To change time with moment.js and JavaScript, we can use the set method.

Categories
JavaScript Answers

How to print an object’s keys and values with JavaScript?

Sometimes, we want to print an object’s keys and values with JavaScript.

In this article, we’ll look at how to print an object’s keys and values with JavaScript.

How to print an object’s keys and values with JavaScript?

To print an object’s keys and values with JavaScript, we can use Object.entries and the for-of loop.

For instance, we write:

const filters = {
  "user": "abc",
  "application": "xyz"
}

for (const [key, val] of Object.entries(filters)) {
  console.log(key, val)
}

We loop through the key-value pairs of filters by convert it to an array of key-value pair arrays with Object.entries.

In the for-of loop, we destructure the key and value from each array entry into key and val and log them.

Therefore, we see:

user abc
application xyz

logged.

Conclusion

To print an object’s keys and values with JavaScript, we can use Object.entries and the for-of loop.

Categories
JavaScript Answers

How to return an empty promise with JavaScript?

To return an empty promise with JavaScript, we can use an async function or call Promise.resolve.

For instance, we write:

const p1 = async () => {}
const p2 = () => Promise.resolve()

to create 2 functions that returns empty promises.

p1 is an async function, so it’s a function that always returns a promise.

And since it has no return value, it returns an empty promise.

p2 is a regular function that returns an empty promise by calling Promise.resolve with no arguments.