Categories
JavaScript Answers

How to make anchor link go some pixels above where it’s linked to with CSS?

Sometimes, we want to make anchor link go some pixels above where it’s linked to with CSS.

In this article, we’ll look at how to make anchor link go some pixels above where it’s linked to with CSS.

How to make anchor link go some pixels above where it’s linked to with CSS?

To make anchor link go some pixels above where it’s linked to with CSS, we set the scroll-margin-top style.

For instance, we write

#anchor {
  scroll-margin-top: 100px;
}

to set the scroll-margin-top to 100px to make the browser scroll 100px above the element where the anchor element is when the link for the anchor element is clicked.

Conclusion

To make anchor link go some pixels above where it’s linked to with CSS, we set the scroll-margin-top style.

Categories
JavaScript Answers

How to display two digit numbers with JavaScript date getMinutes?

Sometimes, we want to display two digit numbers with JavaScript date getMinutes.

In this article, we’ll look at how to display two digit numbers with JavaScript date getMinutes.

How to display two digit numbers with JavaScript date getMinutes?

To display two digit numbers with JavaScript date getMinutes, we can use the string padStart method.

For instance, we write

const minutes = String(date.getMinutes()).padStart(2, "0");

to get the minutes from the date with getMinutes.

We convert the number returned by getMinutes to a string with String.

Then we call padStart with 2 and '0' to prepend '0'‘s to the string to make the string length 2 if needed.

Conclusion

To display two digit numbers with JavaScript date getMinutes, we can use the string padStart method.

Categories
JavaScript Answers

How to get list of data-* attributes using JavaScript?

Sometimes, we want to get list of data-* attributes using JavaScript.

In this article, we’ll look at how to get list of data-* attributes using JavaScript.

How to get list of data-* attributes using JavaScript?

To get list of data-* attributes using JavaScript, we use the element’s dataset property.

For instance, we write

const attributes = element.dataset;

to use the element.dataset object to get the data- attributes of element.

The property keys would be the name after the hyphen.

And the values would be the attribute values.

Conclusion

To get list of data-* attributes using JavaScript, we use the element’s dataset property.

Categories
JavaScript Answers

How to add onChange event for contentEditable elements with React?

Sometimes, we want to add onChange event for contentEditable elements with React.

In this article, we’ll look at how to add onChange event for contentEditable elements with React.

How to add onChange event for contentEditable elements with React?

To add onChange event for contentEditable elements with React, we can listen to the input event on the contentEditable elements.

For instance, we write

<div
  contentEditable="true"
  onInput={(e) => console.log(e.currentTarget.textContent)}
>
  Text inside div
</div>

to add a contentEditable div into our component.

We listen for changes to its contents by setting onInput to a function that runs when the content changes.

Then we get the latest content of the div with e.currentTarget.textContent.

Conclusion

To add onChange event for contentEditable elements with React, we can listen to the input event on the contentEditable elements.

Categories
JavaScript Answers

How to globally replace a forward slash in a JavaScript string?

Sometimes, we want to globally replace a forward slash in a JavaScript string.

In this article, we’ll look at how to globally replace a forward slash in a JavaScript string.

How to globally replace a forward slash in a JavaScript string?

To globally replace a forward slash in a JavaScript string, we call the string replace method with a regex with the g flag.

For instance, we write

const s = "/string/".replace(/\//g, "ForwardSlash");

to call '/string/'.replace with the regex /\//g and 'ForwardSlash' to replace the forward slashes in '/string/' with 'ForwardSlash'.

Conclusion

To globally replace a forward slash in a JavaScript string, we call the string replace method with a regex with the g flag.