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.

Categories
React Answers

How to create button with text under the icon with React Material UI?

Sometimes, we want to create button with text under the icon with React Material UI.

In this article, we’ll look at how to create button with text under the icon with React Material UI.

How to create button with text under the icon with React Material UI?

To create button with text under the icon with React Material UI, we can set the flexDirection of the button container.

For instance, we write:

import React from "react";
import Button from "@material-ui/core/Button";
import Settings from "@material-ui/icons/Settings";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(() => ({
  button: {
    height: 95
  },
  label: {
    flexDirection: "column"
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div>
      <Button
        classes={{ root: classes.button, label: classes.label }}
        variant="raised"
        color="primary"
        disableRipple={true}
      >
        <Settings className={classes.icon} />
        Message
      </Button>
    </div>
  );
}

We call makeStyles with a function that has the label class property that sets flexDirection to 'column'.

Then we set that as the returned classes as the value of the classes prop.

As a result, we should see that ‘Message’ is below the settings icon.

Conclusion

To create button with text under the icon with React Material UI, we can set the flexDirection of the button container.