Categories
JavaScript Answers

How to set the form action through JavaScript?

Sometimes, we want to set the form action through JavaScript.

In this article, we’ll look at how to set the form action through JavaScript.

How to set the form action through JavaScript?

To set the form action through JavaScript, we set the action property.

For instance, we write

document.getElementById("form_id").action = "script.php";

to select the form with getElementById.

Then we set its action property to the action attribute value.

Conclusion

To set the form action through JavaScript, we set the action property.

Categories
JavaScript Answers

How to download image with JavaScript?

Sometimes, we want to download image with JavaScript.

In this article, we’ll look at how to download image with JavaScript.

How to download image with JavaScript?

To download image with JavaScript, we create a link.

For instance, we write

const a = document.createElement("a");
a.href = "img.png";
a.download = "output.png";
a.click();

to create a link with the createElement method.

Then we set its href property to the path to the file we want to download.

Next, we set the download property to the string with the filename of the downloaded file.

Then we call click to download the file.

Conclusion

To download image with JavaScript, we create a link.

Categories
JavaScript Answers

How to remove hash from URL with JavaScript?

Sometimes, we want to remove hash from URL with JavaScript.

In this article, we’ll look at how to remove hash from URL with JavaScript.

How to remove hash from URL with JavaScript?

To remove hash from URL with JavaScript, we call the history.pushState method.

For instance, we write

history.pushState("", document.title, window.location.pathname);

to call pushState with '' and window.location.pathname to remove the hash from the URL.

Conclusion

To remove hash from URL with JavaScript, we call the history.pushState method.

Categories
JavaScript Answers

How to enumerate dates between two dates in Moment and JavaScript?

Sometimes, we want to enumerate dates between two dates in Moment and JavaScript.

In this article, we’ll look at how to enumerate dates between two dates in Moment and JavaScript.

How to enumerate dates between two dates in Moment and JavaScript?

To enumerate dates between two dates in Moment and JavaScript, we use a while loop.

For instance, we write

const enumerateDaysBetweenDates = (startDate, endDate) => {
  const now = startDate.clone();
  const dates = [];

  while (now.isSameOrBefore(endDate)) {
    dates.push(now.format("M/D/YYYY"));
    now.add(1, "days");
  }
  return dates;
};

to define the enumerateDaysBetweenDates function.

In it, we clone startDate with clone.

And then we call now.isSameOrBefore with endDate to check if now is before or same as endDate.

If it is, the while loop runs.

In it, we call dates.push to push the now date formatted into a string into dates.

Then we call now.add to add day tonow`.

Finally, we return the dates array.

Conclusion

To enumerate dates between two dates in Moment and JavaScript, we use a while loop.

Categories
JavaScript Answers

How to convert JavaScript array to CSV?

Sometimes, we want to convert JavaScript array to CSV.

In this article, we’ll look at how to convert JavaScript array to CSV.

How to convert JavaScript array to CSV?

To convert JavaScript array to CSV, we use array and object methods.

For instance, we write

const data = [
  { title: "Book title 1", author: "Name1 Surname1" },
  { title: "Book title 2", author: "Name2 Surname2" },
  { title: "Book title 3", author: "Name3 Surname3" },
  { title: "Book title 4", author: "Name4 Surname4" },
];

const csv = data
  .map((d) => {
    return Object.values(d).join(",");
  })
  .join("\n")

to call data.map with a callback that gets the values from each object in data with Object.values.

Then we call join to join the values into a string with each entry separated by a comma.

Then we call join again to combine the strings into a CSV with each line separated by a new line character.

Conclusion

To convert JavaScript array to CSV, we use array and object methods.