Categories
JavaScript Answers

How to check whether the date entered by the user is current date or the future date with JavaScript?

Sometimes, we want to check whether the date entered by the user is current date or the future date with JavaScript.

In this article, we’ll look at how to check whether the date entered by the user is current date or the future date with JavaScript.

How to check whether the date entered by the user is current date or the future date with JavaScript?

To check whether the date entered by the user is current date or the future date with JavaScript, we can set the time to midnight on each date and compare them.

For instance, we write:

const inFuture = (date) => {
  return date.setHours(0, 0, 0, 0) > new Date().setHours(0, 0, 0, 0)
};

console.log(inFuture(new Date(2029, 1, 1)))
console.log(inFuture(new Date(2019, 1, 1)))

to define the inFuture function that takes a date.

In the function, we call setHours with all 0’s to set the dates to midnight of the same date.

Then we check if date is bigger than new Date() to check if date is in the future.

Therefore, if today is January 21, 2022, then the first console log is true and the 2nd log is false.

Conclusion

To check whether the date entered by the user is current date or the future date with JavaScript, we can set the time to midnight on each date and compare them.

Categories
JavaScript Answers

How to use setAttribute to set multiple classes on an element with JavaScript?

Sometimes, we want to use setAttribute to set multiple classes on an element with JavaScript.

In this article, we’ll look at how to use setAttribute to set multiple classes on an element with JavaScript.

How to use setAttribute to set multiple classes on an element with JavaScript?

To use setAttribute to set multiple classes on an element with JavaScript, we can call setAttribute with all the class names separated by a space each.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

const element = document.querySelector('div')
element.setAttribute("class", "class1 class2");

to select the div with querySelector.

Then we call setAttribute with 'class' and 'class1 class2' to set the class attribute of the div to class1 class2.

Conclusion

To use setAttribute to set multiple classes on an element with JavaScript, we can call setAttribute with all the class names separated by a space each.

Categories
JavaScript Answers

How to run code after the page has loaded completely with JavaScript?

Sometimes, we want to run code after the page has loaded completely with JavaScript.

In this article, we’ll look at how to run code after the page has loaded completely with JavaScript.

How to run code after the page has loaded completely with JavaScript?

To run code after the page has loaded completely with JavaScript, we can run it in the window.onload method.

For instance, we write:

window.onload = () => {
  console.log('page loaded')
}

to set the window.onload property to a function that runs when the page is completely loaded.

Therefore, we see 'page loaded' logged when the page is loaded completely.

Conclusion

To run code after the page has loaded completely with JavaScript, we can run it in the window.onload method.

Categories
JavaScript Answers

How to percent encode strings with JavaScript?

Sometimes, we want to percent encode strings with JavaScript.

In this article, we’ll look at how to percent encode strings with JavaScript.

How to percent encode strings with JavaScript?

To percent encode strings with JavaScript, we can use the escape function.

For instance, we write:

console.log(escape("Ω"))

And we see '%u03A9' logged.

Conclusion

To percent encode strings with JavaScript, we can use the escape function.

Categories
JavaScript Answers

How to get values from inputs and create an array with JavaScript?

Sometimes, we want to get values from inputs and create an array with JavaScript.

In this article, we’ll look at how to get values from inputs and create an array with JavaScript.

How to get values from inputs and create an array with JavaScript?

To get values from inputs and create an array with JavaScript, we can get all the inputs and get each input’s value from the value property.

For instance, we write:

<form>
  <input type="text">
  <input type="text">
  <input type="text">
  <input type='submit'>
</form>

to add a form.

then we write:

const form = document.querySelector('form')
const input = document.querySelectorAll('input[type="text"]')
form.onsubmit = (e) => {
  e.preventDefault()
  const arr = [...input].map(i => i.value)
  console.log(arr)
}

to select the form with querySelector and select the text inputs with querySelectorAll.

Then we set the form.onsubmit property to a function that runs when the form is submitted.

In the function, we call e.preventDefault to prevent the default server side form submission behavior.

Then we get all the input values by spreading the inputs into an array and call map with a callback that returns the value property of each input.

As a result, when we submit the form with input values, we see them logged.

Conclusion

To get values from inputs and create an array with JavaScript, we can get all the inputs and get each input’s value from the value property.