Categories
JavaScript Answers

How to check if a cookie exists or else set cookie to expire in 10 days with JavaScript?

Sometimes, we want to check if a cookie exists or else set cookie to expire in 10 days with JavaScript.

In this article, we’ll look at how to check if cookie a exists or else set cookie to expire in 10 days with JavaScript.

How to check if cookie a exists or else set cookie to expire in 10 days with JavaScript?

To check if a cookie exists or else set cookie to expire in 10 days with JavaScript, we can read and write the to the document.cookie property.

For instance, we write:

if (/(^|;)\s*visited=/.test(document.cookie)) {
  console.log("Hello again!");
} else {
  document.cookie = "visited=true; max-age=" + 60 * 60 * 24 * 10;
  console.log("first visit");
}

We check if the cookie string has the visit key with /(^|;)\s*visited=/.test(document.cookie).

If it’s true, we log 'Hello again'.

Otherwise, we assign document.cookie to "visited=true; max-age=" + 60 * 60 * 24 * 10 to add a cookie with key visited that expires in 10 days.

Conclusion

To check if a cookie exists or else set cookie to expire in 10 days with JavaScript, we can read and write the to the document.cookie property.

Categories
JavaScript Answers

How to convert HH:MM:SS times to seconds with moment.js?

Sometimes, we want to convert HH:MM:SS times to seconds with moment.js.

In this article, we’ll look at how to convert HH:MM:SS times to seconds with moment.js.

How to convert HH:MM:SS times to seconds with moment.js?

To convert HH:MM:SS times to seconds with moment.js, we can use the diff and startOf methods.

For instance, we write:

const s = moment('12:10:12: PM', 'HH:mm:ss: A').diff(moment().startOf('day'), 'seconds');
console.log(s)

We create a moment object with moment by parsing the time string.

Then we call diff to find the difference between the start of the day and the given time and return the result in seconds.

Therefore s is 43812.

Conclusion

To convert HH:MM:SS times to seconds with moment.js, we can use the diff and startOf methods.

Categories
JavaScript Answers

How to capture all the anchor element click events with JavaScript?

Sometimes, we want to capture all the anchor element click events with JavaScript.

In this article, we’ll look at how to capture all the anchor element click events with JavaScript.

How to capture all the anchor element click events with JavaScript?

To capture all the anchor element click events with JavaScript, we can use event delegation.

For instance, we write:

<a href='https://example.com/foo'>foo</a>
<a href='https://example.com/bar'>bar</a>

to add 2 anchor elements.

Then we write:

document.addEventListener(`click`, e => {
  if (e.target.tagName.toLowerCase() === 'a') {
    e.preventDefault()
    console.log(`You clicked ${e.target.href}`);
  }
});

to call document.addEventListener to add an event listener to the whole page.

Then we check the tag name of the element is clicked on with e.target.tagName.

If it’s 'a', then we log the href property of it, which has the value of the href attribute of the anchor element.

Conclusion

To capture all the anchor element click events with JavaScript, we can use event delegation.

Categories
JavaScript Answers

How to get month in mm format in JavaScript?

Sometimes, we want to get month in mm format in JavaScript.

In this article, we’ll look at how to get month in mm format in JavaScript.

How to get month in mm format in JavaScript?

To get month in mm format in JavaScript, we can use the date’s getMonth and string’s padStart methods.

For instance, we write:

const mm = (new Date(2022, 1, 1).getMonth() + 1).toString().padStart(2, '0')
console.log(mm)

to create a new date set to Feb 1, 2022.

We get the month number from the date with getMonth

And we add 1 to it to convert it to a human readable month number.

Then we call toString to convert it to a string.

And then we all padStart with 2 and '0' to prepend 0’s to the string until it has length 2.

Therefore, mm is '02'.

Conclusion

To get month in mm format in JavaScript, we can use the date’s getMonth and string’s padStart methods.

Categories
CSS HTML

How to restrict max text area width and height?

Sometimes, we want to restrict max text area width and height.

In this article, we’ll look at how to restrict max text area width and height.

How to restrict max text area width and height?

To restrict max text area width and height, we can set the resize, max-height and max-width CSS properties.

For instance, we write:

<textarea style='resize: vertical; max-width: 200px; max-height: 200px;'></textarea>

to add a textarea element.

We set the resize CSS property to vertical to allow vertical resizing only.

And we set the max-width and max-height properties to restrict the size of the textarea.

Conclusion

To restrict max text area width and height, we can set the resize, max-height and max-width CSS properties.