Categories
JavaScript Answers

How to get HTML form values with JavaScript?

Sometimes, we want to get HTML form values with JavaScript.

In this article, we’ll look at how to get HTML form values with JavaScript.

How to get HTML form values with JavaScript?

To get HTML form values with JavaScript, we use the FormData constructor with the form element.

For instance, we write

const handleSubmit = (e) => {
  e.preventDefault();
  const formData = new FormData(e.target);
  const formProps = Object.fromEntries(formData);
};

to create the handleSubmit function that calls e.preventDefault to prevent the default server side submission behavior.

Then we create a FormData object with e.target.

And then we convert the formData object that has the form values to an object with Object.fromEntries.

Conclusion

To get HTML form values with JavaScript, we use the FormData constructor with the form element.

Categories
JavaScript Answers

How to determine one year from now in JavaScript?

Sometimes, we want to determine one year from now in JavaScript.

In this article, we’ll look at how to determine one year from now in JavaScript.

How to determine one year from now in JavaScript?

To determine one year from now in JavaScript, we call the getFullYear and setFullYear method.

For instance, we write

const d = new Date(new Date().setFullYear(new Date().getFullYear() + 1));

to get the 4 digit year of the current date time with

new Date().getFullYear()

Then we add 1 to it and call setFullYear with the sum to set the date to be 1 year from now.

And create a new Date object with the same date time by using Date with

new Date().setFullYear(new Date().getFullYear() + 1)

Conclusion

To determine one year from now in JavaScript, we call the getFullYear and setFullYear method.

Categories
JavaScript Answers

How to hide the legend in Google Chart with JavaScript?

Sometimes, we want to hide the legend in Google Chart with JavaScript

In this article, we’ll look at how to hide the legend in Google Chart with JavaScript.

How to hide the legend in Google Chart with JavaScript?

To hide the legend in Google Chart with JavaScript, we set the legend property to 'none'.

For instance, we write

const options = {
  //...
  legend: "none",
};

to set legend to 'none' in the options object to hide the legend.

Conclusion

To hide the legend in Google Chart with JavaScript, we set the legend property to 'none'.

Categories
JavaScript Answers

How to force page scroll position to top at page refresh in HTML with JavaScript?

Sometimes, we want to force page scroll position to top at page refresh in HTML with JavaScript.

In this article, we’ll look at how to force page scroll position to top at page refresh in HTML with JavaScript.

How to force page scroll position to top at page refresh in HTML with JavaScript?

To force page scroll position to top at page refresh in HTML with JavaScript, we scroll the page to the top when the beforeunload event is triggered.

For instance, we write

window.onbeforeunload = () => {
  window.scrollTo(0, 0);
};

to set window.onbeforeunlod to a function that runs when we refresh the page.

In it, we call scrollTo with 0 and 0 to scroll to the top of the page when we refresh.

Conclusion

To force page scroll position to top at page refresh in HTML with JavaScript, we scroll the page to the top when the beforeunload event is triggered.

Categories
JavaScript Answers

How to go to a specific element on page with JavaScript?

Sometimes, we want to go to a specific element on page with JavaScript.

In this article, we’ll look at how to go to a specific element on page with JavaScript.

How to go to a specific element on page with JavaScript?

To go to a specific element on page with JavaScript, we can use the element’s scrollIntoView method.

For instance, we write

document.getElementById("elementID").scrollIntoView();

to get the element we want to scroll to with

document.getElementById("elementID")

Then we call scrollIntoView to scroll to the element.

Conclusion

To go to a specific element on page with JavaScript, we can use the element’s scrollIntoView method.