Categories
JavaScript Answers

How to loop through all descendants of a div with JavaScript?

Sometimes, we want to loop through all descendants of a div with JavaScript.

In this article, we’ll look at how to loop through all descendants of a div with JavaScript.

How to loop through all descendants of a div with JavaScript?

To loop through all descendants of a div with JavaScript, we can select all the descendants with getElementsByTagName.

For instance, we write:

<div id='id'>
  <p>
    bar
  </p>
  <p>
    baz
  </p>
  <p>
    foo
  </p>
</div>

to add a div with some descendant elements.

Then we select the descendants and loop through them with:

const ancestor = document.getElementById('id')
const descendents = ancestor.getElementsByTagName('*');

for (const d of descendents) {
  console.log(d)
}

We select the div with ID id with getElementById.

Then we call ancestor.getElementsByTagName with '*' to select all the descendant elemnts from ancestor.

Finally, we loop through them with a for-of loop.

Therefore, we should see all the p elements logged.

Conclusion

To loop through all descendants of a div with JavaScript, we can select all the descendants with getElementsByTagName.

Categories
JavaScript Answers

How to remove focus from submit button with JavaScript?

Sometimes, we want to remove focus from submit button with JavaScript

In this article, we’ll look at how to remove focus from submit button with JavaScript.

How to remove focus from submit button with JavaScript?

To remove focus from submit button with JavaScript, we can call the button’s blur method.

For instance, we write:

<form>
  <input type="submit">
</form>

to add a form.

Then we write:

const form = document.querySelector('form')
const submit = document.querySelector('input[type="submit"]')

form.onready = () => {
  submit.blur()
}

We select the form and the submit button with querySelector.

Then we call submit.blur when the form is loaded by setting form.onready to a function that calls blur.

Conclusion

To remove focus from submit button with JavaScript, we can call the button’s blur method.

Categories
JavaScript Answers

How to merge two dates in JavaScript?

Sometimes, we want to merge two dates in JavaScript.

In this article, we’ll look at how to merge two dates in JavaScript.

How to merge two dates in JavaScript?

To merge two dates in JavaScript, we can get the parts from each date and put it in a new date.

For instance, we write:

const date = new Date('Wed Dec 21 2022 00:00:00 GMT+0000 (GMT)')
const time = new Date('Sun Dec 31 2000 03:00:00 GMT+0000 (GMT)')
const datetime = new Date(date.getFullYear(), date.getMonth(), date.getDate(),
  time.getHours(), time.getMinutes(), time.getSeconds());
console.log(datetime)

to combine the date and time into datetime.

We get the year, month, and day from date with getFullYear, getMonth, and getHours.

And then we get the hours, minutes, and seconds with getHours, getMinuts, and getSeconds.

As a result, we get that datetime is 'Tue Dec 20 2022 19:00:00 GMT-0800 (Pacific Standard Time) '.

Conclusion

To merge two dates in JavaScript, we can get the parts from each date and put it in a new date.

Categories
React Answers

How to clear the Material UI text field Value in React?

Sometimes, we want to clear the Material UI text field Value in React.

In the article, we’ll look at how to clear the Material UI text field Value in React.

How to clear the Material UI text field Value in React?

To clear the Material UI text field Value in React, we can set the value property of the input to an empty string.

For instance, we write:

import React from "react";
import Button from "@material-ui/core/Button";
import TextField from "@material-ui/core/TextField";

export default function App() {
  const textInput = React.useRef(null);

  return (
    <div>
      <Button
        onClick={() => {
          textInput.current.value = "";
        }}
      >
        clear
      </Button>
      <TextField
        fullWidth
        required
        inputRef={textInput}
        name="firstName"
        type="text"
        placeholder="Enter Your First Name"
        label="First Name"
      />
    </div>
  );
}

to add a button and a text field.

We assigned the textInput ref to the TextField so we can its input value to an empty string when we click on the Button.

And we set the input value of the TextField to an empty string with textInput.current.value = "".

Conclusion

To clear the Material UI text field Value in React, we can set the value property of the input to an empty string.

Categories
React Answers

How to remove the scrollbar from the React Material UI dialog?

Sometimes, we want to remove the scrollbar from the React Material UI dialog.

In this article, we’ll look at how to remove the scrollbar from the React Material UI dialog.

How to remove the scrollbar from the React Material UI dialog?

To remove the scrollbar from the React Material UI dialog, we can set the style prop of the DialogContent component.

For instance, we write:

import React from "react";
import Dialog from "@material-ui/core/Dialog";
import DialogContent from "@material-ui/core/DialogContent";

export default function App() {
  return (
    <div>
      <Dialog fullWidth={true} maxWidth="xl" open={true}>
        <DialogContent style={{ overflow: "hidden" }}>
          <div>hello world</div>
        </DialogContent>
      </Dialog>
    </div>
  );
}

to set the style prop to an object that sets the overflow CSS property to 'hidden'.

This will hide the scrollbar regardless of content size.

Conclusion

To remove the scrollbar from the React Material UI dialog, we can set the style prop of the DialogContent component.