Categories
JavaScript Answers

How to prevent typing non-numeric characters in input type number with JavaScript?

Sometimes, we want to prevent typing non-numeric characters in input type number with JavaScript.

In this article, we’ll look at how to prevent typing non-numeric characters in input type number with JavaScript.

How to prevent typing non-numeric characters in input type number with JavaScript?

To prevent typing non-numeric characters in input type number with JavaScript, we can listen for keypress events.

For instance, we write

document.querySelector("input").addEventListener("keypress", (evt) => {
  if (evt.which < 48 || evt.which > 57) {
    evt.preventDefault();
  }
});

to select the input with querySelector.

Then we call addEventListener to listen to the keypress event.

In the event handler, we check if non-numeric keys are pressed with evt.which < 48 || evt.which > 57.

If it’s true, then we call preventDefault to stop the character from being appended into the value.

Conclusion

To prevent typing non-numeric characters in input type number with JavaScript, we can listen for keypress events.

Categories
JavaScript Answers

How to get the DOM element value using pure JavaScript?

Sometimes, we want to get the DOM element value using pure JavaScript.

In this article, we’ll look at how to get the DOM element value using pure JavaScript.

How to get the DOM element value using pure JavaScript?

To get the DOM element value using pure JavaScript, we use the value property.

For instance, we write

<input id="theId" value="test" />

to add an input.

Then we get the input’s value attribute value with

const value = document.getElementById("theId").value;

We select the input with getElementById.

Then we get its value property to get the input’s value attribute value.

Conclusion

To get the DOM element value using pure JavaScript, we use the value property.

Categories
JavaScript Answers

How to convert JavaScript associative array to JSON?

Sometimes, we want to convert JavaScript associative array to JSON.

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

How to convert JavaScript associative array to JSON?

To convert JavaScript associative array to JSON, we can use the JSON.stringify method.

For instance, we write

const assocArray = {};
assocArray["a"] = "The letter A";
const json = JSON.stringify(assocArray);

to call JSON.stringify with assocArray to return the stringified version of the assocArray object as a JSON string.

Conclusion

To convert JavaScript associative array to JSON, we can use the JSON.stringify method.

Categories
JavaScript Answers

How to make enter key press behaves like a tab key in JavaScript?

Sometimes, we want to make enter key press behaves like a tab key in JavaScript.

In this article, we’ll look at how to make enter key press behaves like a tab key in JavaScript.

How to make enter key press behaves like a tab key in JavaScript?

To make enter key press behaves like a tab key in JavaScript, we can listen for the keydown event.

For instance, we write

document.addEventListener("keydown", (event) => {
  if (event.keyCode === 13 && event.target.nodeName === "INPUT") {
    const form = event.target.form;
    const index = [...form].indexOf(event.target);
    form.elements[index + 1].focus();
    event.preventDefault();
  }
});

to listen for the keydown event on document with addEventListener.

In the event listener, we check if the enter key is pressed by checking if event.keyCode is 13.

And we check if the key is pressed in an input with

event.target.nodeName === "INPUT"

If both are true, then we get the form the input is in with event.target.form.

Then we get the index of the input with [...form].indexOf(event.target).

Next, we focus on the next input with form.elements[index + 1].focus().

And we call preventDefault to stop the default behavior.

Conclusion

To make enter key press behaves like a tab key in JavaScript, we can listen for the keydown event.

Categories
JavaScript Answers

How to compare two string dates in JavaScript?

Sometimes, we want to compare two string dates in JavaScript.

In this article, we’ll look at how to compare two string dates in JavaScript.

How to compare two string dates in JavaScript?

To compare two string dates in JavaScript, we can compare them after we convert the date strings to timestamps.

For instance, we write

const isLater = (str1, str2) => {
  return new Date(str1) > new Date(str2);
};

to define the isLater function.

In it, we convert str1 and str2 to Date objects.

Then we compare them directly with >.

The Date objects are converted to timestamps automatically before they’re compared.

Conclusion

To compare two string dates in JavaScript, we can compare them after we convert the date strings to timestamps.