Categories
JavaScript Answers

How to get tomorrow’s date in format dd-mm-yy with JavaScript?

Sometimes, we want to get tomorrow’s date in format dd-mm-yy with JavaScript.

In this article, we’ll look at how to get tomorrow’s date in format dd-mm-yy with JavaScript.

How to get tomorrow’s date in format dd-mm-yy with JavaScript?

To get tomorrow’s date in format dd-mm-yy with JavaScript, we can use the date instance’s toLocaleDateString method.

For instance, we write

const d = new Date(2022, 1, 29);
console.log(d.toLocaleDateString());

to create date d with the Date constructor.

Then we call toLocaleDateString to return a human readable date string.

Conclusion

To get tomorrow’s date in format dd-mm-yy with JavaScript, we can use the date instance’s toLocaleDateString method.

Categories
JavaScript Answers

How to reprompt for permissions with getUserMedia() after initial denial with JavaScript?

Sometimes, we want to reprompt for permissions with getUserMedia() after initial denial with JavaScript.

In this article, we’ll look at how to reprompt for permissions with getUserMedia() after initial denial with JavaScript.

How to reprompt for permissions with getUserMedia() after initial denial with JavaScript?

To reprompt for permissions with getUserMedia() after initial denial with JavaScript, we can call the navigator.permissions.query method.

For instance, we write

const permissionObj = await navigator.permissions.query({ name: "microphone" });

to call navigator.permissions.query in an async function with an object with the name property set to a string with the permission that we want get.

The user will then be prompted with the permission prompt and permissionObj will have the user’s choice once the prompt is closed.

query returns a promise so we use await to get permissionObj.

Conclusion

To reprompt for permissions with getUserMedia() after initial denial with JavaScript, we can call the navigator.permissions.query method.

Categories
JavaScript Answers

How to change text node value with JavaScript?

Sometimes, we want to change text node value with JavaScript.

In this article, we’ll look at how to change text node value with JavaScript.

How to change text node value with JavaScript?

To change text node value with JavaScript, we can set the nodeValue property.

For instance, we write

node.nodeValue = "new value";

to set the text node‘s nodeValue to a string.

Setting nodeValue will only set the value of the text node and not affect descending nodes.

Conclusion

To change text node value with JavaScript, we can set the nodeValue property.

Categories
React Answers

How to access store state in React Redux and JavaScript?

Sometimes, we want to access store state in React Redux and JavaScript.

In this article, we’ll look at how to access store state in React Redux and JavaScript.

How to access store state in React Redux and JavaScript?

To access store state in React Redux and JavaScript, we can use the useSelector hook.

For instance, we write

import { useSelector } from "react-redux";

export const useEmployees = () => {
  return useSelector((state) => state.employees);
};

to create the useEmployees hook.

In it, we call the useSelector hook that select the employees state from the Redux store.

Then, we write

const Comp = () => {
  const { employees } = useEmployees();
  //...
};

to call the useEmployees hook in the Comp component to get the employees from the store.

Conclusion

To access store state in React Redux and JavaScript, we can use the useSelector hook.

Categories
JavaScript Answers

How to move all HTML element children to another parent using JavaScript?

Sometimes, we want to move all HTML element children to another parent using JavaScript.

In this article, we’ll look at how to move all HTML element children to another parent using JavaScript.

How to move all HTML element children to another parent using JavaScript?

To move all HTML element children to another parent using JavaScript, we can use the appendChild method.

For instance, we write

const setParent = (el, newParent) => {
  newParent.appendChild(el);
};

const l = document.getElementById("old-parent").childNodes.length;
const a = document.getElementById("old-parent");
const b = document.getElementById("new-parent");
for (let i = l; i >= 0; i--) {
  setParent(a.childNodes[i], b);
}

to define the setParent function to new el as a child of newParent.

Then we select the old parent and assign it to a and select the new parent and assign it to b.

Next we use a for loop to loop through a‘s childNodes and move them to b with setParent.

We loop through the childNodes backward so that the order of the child nodes left in a won’t be affected by the loop.

Conclusion

To move all HTML element children to another parent using JavaScript, we can use the appendChild method.