Categories
JavaScript Answers

How to set the value of a input hidden field through JavaScript?

To set the value of a input hidden field through JavaScript, we set the input’s value property.

For instance, we write

document.getElementById("checkyear").value = "1";

to select the input with getElementById.

Then we set its value property to the input value.

Categories
JavaScript Answers

How to save output to variable as an object with JavaScript Fetch API?

Sometimes, we want to save output to variable as an object with JavaScript Fetch API.

In this article, we’ll look at how to save output to variable as an object with JavaScript Fetch API.

How to save output to variable as an object with JavaScript Fetch API?

To save output to variable as an object with JavaScript Fetch API, we call res.json.

For instance, we write

const foo = async () => {
  const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
  const obj = await res.json();
  console.log(obj);
};

foo();

to call fetch to make a get request to https://jsonplaceholder.typicode.com/posts/1.

Then we get the promise with the response body with res.json.

And we get response body with await.

Conclusion

To save output to variable as an object with JavaScript Fetch API, we call res.json.

Categories
JavaScript Answers

How to validate if date is before date of current date with JavaScript?

Sometimes, we want to validate if date is before date of current date with JavaScript.

In this article, we’ll look at how to validate if date is before date of current date with JavaScript.

How to validate if date is before date of current date with JavaScript?

To validate if date is before date of current date with JavaScript, we compare their timestamps.

For instance, we write

const isAfterToday = (date) => {
  return new Date(date).valueOf() > new Date().valueOf();
};

to define the isAfterToday function.

In it, we compare the date date with today’s date by converting to timestamps with the valueOf method.

Then we compare if date is bigger than today’s date with >.

Conclusion

To validate if date is before date of current date with JavaScript, we compare their timestamps.

Categories
JavaScript Answers

How to set just the date, ignoring time with JavaScript Date?

Sometimes, we want to set just the date, ignoring time with JavaScript Date.

In this article, we’ll look at how to set just the date, ignoring time with JavaScript Date.

How to set just the date, ignoring time with JavaScript Date?

To set just the date, ignoring time with JavaScript Date, we pass in a date string into the Date constructor.

For instance, we write

const newDate = new Date(oldDate.toDateString());

to get the oldDate‘s date as a string with toDateString.

Then we pass that into the Date constructor to create a new date object with the date.

Conclusion

To set just the date, ignoring time with JavaScript Date, we pass in a date string into the Date constructor.

Categories
JavaScript Answers

How to check whether a button is clicked by using JavaScript?

Sometimes, we want to check whether a button is clicked by using JavaScript.

In this article, we’ll look at how to check whether a button is clicked by using JavaScript.

How to check whether a button is clicked by using JavaScript?

To check whether a button is clicked by using JavaScript, we add a click listener to the button.

For instance, we write

document.getElementById("button").onclick = () => {
  console.log("button was clicked");
};

to select the button with getElementById.

Then we set its onclick property to a function that’s called when the button is clicked.

Conclusion

To check whether a button is clicked by using JavaScript, we add a click listener to the button.