Categories
JavaScript Answers

How to auto-scroll to end of div when data is added with JavaScript?

Sometimes, we want to auto-scroll to end of div when data is added with JavaScript.

In this article, we’ll look at how to auto-scroll to end of div when data is added with JavaScript.

How to auto-scroll to end of div when data is added with JavaScript?

To auto-scroll to end of div when data is added with JavaScript, we can set the scrollTop of the div to the scrollHeight of the div.

For instance, we write

window.setInterval(() => {
  const elem = document.getElementById("data");
  elem.scrollTop = elem.scrollHeight;
}, 5000);

to call setInterval to run a function that scrolls the div with ID data to the bottom.

We get the div with getElementById.

And we set elem.scrollTop to elem.scrollHeight to scroll the div to the bottom.

Conclusion

To auto-scroll to end of div when data is added with JavaScript, we can set the scrollTop of the div to the scrollHeight of the div.

Categories
React Answers

How to register event with React useEffect hooks?

Sometimes, we want to register event with React useEffect hooks.

In this article, we’ll look at how to register event with React useEffect hooks.

How to register event with React useEffect hooks

To register event with React useEffect hooks, we can call the window.addEventListener method in the useEffect callback.

For instance, we write

//...

const Comp = () => {
  const [userText, setUserText] = useState("");
  const handleUserKeyPress = useCallback((event) => {
    const { key, keyCode } = event;
    if (keyCode === 32 || (keyCode >= 65 && keyCode <= 90)) {
      setUserText((prevUserText) => `${prevUserText}${key}`);
    }
  }, []);

  useEffect(() => {
    window.addEventListener("keydown", handleUserKeyPress);
    return () => {
      window.removeEventListener("keydown", handleUserKeyPress);
    };
  }, [handleUserKeyPress]);

  return (
    <div>
      <blockquote>{userText}</blockquote>
    </div>
  );
};

to create the userText state with the useState hook.

Then we creatr the handleUserKeyPress function that checks for the key that’s pressed.

And we call setUserText to append the key pressed to userText.

Then we call useEffect with a callback that calls window.addEventListener to listen to the keydown event.

And handleUserKeyPress is called when the keydown event is triggered.

The useEffect callback is run when handleUserKeyPress changes.

We return a function that calls window.removeEventListener to remove the keydown listener when handleUserKeyPress changes.

Conclusion

To register event with React useEffect hooks, we can call the window.addEventListener method in the useEffect callback.

Categories
TypeScript Answers

How to format a date or time in TypeScript?

Sometimes, we want to format a date or time in TypeScript.

In this article, we’ll look at how to format a date or time in TypeScript.

How to format a date or time in TypeScript?

To format a date or time in TypeScript, we can use the date’s toLocaleString method.

For instance, we write

const s = new Date().toLocaleString();

to create a new Date object and then call toLocaleString on it to return a string that has the formatted datetime of the current time formatted according to the current locale.

Conclusion

To format a date or time in TypeScript, we can use the date’s toLocaleString method.

Categories
JavaScript Answers

How to use things other than arrays in nested JSON objects?

Sometimes, we want to use things other than arrays in nested JSON objects.

In this article, we’ll look at how to use things other than arrays in nested JSON objects.

How to use things other than arrays in nested JSON objects?

To use things other than arrays in nested JSON objects, we can use objects and primitive values.

For instance, we write

{
  "data": [
    {
      "stuff": [
        {
          "onetype": [
            { "id": 1, "name": "John Doe" },
            { "id": 2, "name": "Don Joeh" }
          ]
        },
        { "othertype": [{ "id": 2, "company": "ACME" }] }
      ]
    },
    {
      "otherstuff": [
        {
          "thing": [
            [1, 42],
            [2, 2]
          ]
        }
      ]
    }
  ]
}

to create JSON objects that has arrays, objects, and primitive values.

Conclusion

To use things other than arrays in nested JSON objects, we can use objects and primitive values.

Categories
JavaScript Answers

How to check a radio button with JavaScript?

Sometimes, we want to check a radio button with JavaScript.

In this article, we’ll look at how to check a radio button with JavaScript.

How to check a radio button with JavaScript?

To check a radio button with JavaScript, we can set the checkbox’s checked property to true.

For instance, we write

<input type="radio" name="main-categories" id="_1234" value="1234" />
<input type="radio" name="main-categories" id="_2345" value="2345" />
<input type="radio" name="main-categories" id="_3456" value="3456" />
<input type="radio" name="main-categories" id="_4567" value="4567" />

to add 4 radio buttons.

Then we write

document.getElementById("_1234").checked = true;

to select the checkbox we want to check with getElementById.

And we set its checked property to true to check it.

Conclusion

To check a radio button with JavaScript, we can set the checkbox’s checked property to true.