Categories
JavaScript Answers

How to generate random SHA1 hash to use as ID in Node.js?

To generate random SHA1 hash to use as ID in Node.js, we use the crypto.randomBytes method.

For instance, we write

const id = crypto.randomBytes(20).toString("hex");

to call randomBytes to create a byte object with 20 bytes.

Then we call toString on it with 'hex' to convert it to a hex string.

Categories
JavaScript Answers

How to set HTML5 required attribute in JavaScript?

To set HTML5 required attribute in JavaScript, we set the required property.

For instance, we write

document.getElementById("edName").required = true;

to select the input with getElementById.

And then we set its required property to true to make it a required input.

Categories
CSS

How to disable a href link in CSS?

To disable a href link in CSS, we set the pointer-event property.

For instance, we write

a {
  pointer-events: none;
}

to disable links by setting pointer-events to none.

Categories
JavaScript Answers

How to target an elements parent element using event.target with JavaScript?

To target an elements parent element using event.target with JavaScript, we use the parentNode property.

For instance, we write

const handleEvent = (e) => {
  const parent = e.currentTarget.parentNode;
};

to define the handleEvent function.

It it, we get the current element that triggered the event with e.currentTarget.

And we get its parent node with the parentNode property.

Categories
JavaScript Answers

How to make a non-cached request with fetch and JavaScript?

To make a non-cached request with fetch and JavaScript, we set the cache option.

For instance, we write

const res = await fetch("some.json", { cache: "no-store" });

to call fetch to make a request to get some.json with a get request.

We set cache to 'no-store' to bypass the cache and get the latest version of some.json.