Categories
JavaScript Answers

How to scroll long page to div with JavaScript?

Sometimes, we want to scroll long page to div with JavaScript.

In this article, we’ll look at how to scroll long page to div with JavaScript.

How to scroll long page to div with JavaScript?

To scroll long page to div with JavaScript, we can select the element to scroll to and call scrollToView on it.

For instance, we write

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

to select the element with ID youridhere with getElementById.

Then we call scrollIntoView to scroll to the element.

Conclusion

To scroll long page to div with JavaScript, we can select the element to scroll to and call scrollToView on it.

Categories
JavaScript Answers

How to send emails with JavaScript?

Sometimes, we want to send emails with JavaScript.

In this article, we’ll look at how to send emails with JavaScript.

How to send emails with JavaScript?

To send emails with JavaScript, we can create a mailto URL and go to it.

For instance, we write

const sendMail = () => {
  const link =
    "mailto:me@example.com" +
    "?cc=myCCaddress@example.com" +
    "&subject=" +
    encodeURIComponent("This is my subject") +
    "&body=" +
    encodeURIComponent(document.getElementById("myText").value);
  window.location.href = link;
};

to define the sendMail function that creates the link mailto URL.

mailto: has the address to send the email to.

cc has the CC email.

subject has the subject.

body has the email body.

We use encodeURIComponent to encode the values so that they can be in the URL.

Then we set window.location.href to link to open the default mail client with the data from the URL filled as the value in the fields.

Conclusion

To send emails with JavaScript, we can create a mailto URL and go to it.

Categories
JavaScript Answers

How to get all the cookies from the browser with JavaScript?

Sometimes, we want to get all the cookies from the browser with JavaScript.

In this article, we’ll look at how to get all the cookies from the browser with JavaScript.

How to get all the cookies from the browser with JavaScript?

To get all the cookies from the browser with JavaScript, we can get them from the document.cookie string.

For instance, we write

const c = document.cookie.split(";").reduce((ac, cv, i) => {
  const [key, val] = cv.split("=");
  return Object.assign(ac, { [key]: val });
}, {});

console.log(c);

to get the cookie string with document.cookie.

Then we split the string by the semicolons and return an array of cookie strings.

Next, we call reduce with a callback that splits the cv string into the key and val strings with split.

And we return an object that merges the { [key]: val } object into the ac object with Object.assign.

Then c is an object with all the cookies with the cookie keys as property keys and the corresponding values as values.

Conclusion

To get all the cookies from the browser with JavaScript, we can get them from the document.cookie string.

Categories
JavaScript Answers

How to remove commas from string using JavaScript?

Sometimes, we want to remove commas from string using JavaScript.

In this article, we’ll look at how to remove commas from string using JavaScript.

How to remove commas from string using JavaScript?

To remove commas from string using JavaScript, we can use the string replace method.

For instance, we write

const total = parseFloat("100,000.00".replace(/,/g, ""));

to return a string that has the commas removed from "100,000.00" by calling replace on it with the /,/g regex and an empty string.

The g flag searches for all instances of the pattern.

Then we call parseFloat with the returned string to convert the string to a number.

Conclusion

To remove commas from string using JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to convert dd/mm/yyyy string into a JavaScript Date object?

Sometimes, we want to convert dd/mm/yyyy string into a JavaScript Date object.

In this article, we’ll look at how to convert dd/mm/yyyy string into a JavaScript Date object.

How to convert dd/mm/yyyy string into a JavaScript Date object?

To convert dd/mm/yyyy string into a JavaScript Date object, we can pass the string straight into the Date constructor.

For instance, we write

const dateString = "10/23/2022";
const dateObject = new Date(dateString);

to create a Date object from the dateString string.

dateString is in dd/mm/yyyy format.

Conclusion

To convert dd/mm/yyyy string into a JavaScript Date object, we can pass the string straight into the Date constructor.