Categories
JavaScript Answers

How to set custom HTML5 required field validation message with JavaScript?

Sometimes, we want to set custom HTML5 required field validation message with JavaScript.

In this article, we’ll look at how to set custom HTML5 required field validation message with JavaScript.

How to set custom HTML5 required field validation message with JavaScript?

To set custom HTML5 required field validation message with JavaScript, we can call the setCustomValidity method.

For instance, we write

<input type="text" name="email" id="emailElement" required />

to add an input element.

Then we write

const emailElement = document.getElementById("emailElement");

emailElement.addEventListener(
  "invalid",
  (e) => {
    const message = `${e.target.value}is not a valid email address`;
    emailElement.setCustomValidity(message);
  },
  false
);

to select the input with getElementById.

And then we call emailElement.addEventListener to listen for the invalid event.

In the event callback, we call emailElement.setCustomValidity with the validation error message.

Conclusion

To set custom HTML5 required field validation message with JavaScript, we can call the setCustomValidity method.

Categories
JavaScript Answers

How to change the current URL with JavaScript?

Sometimes, we want to change the current URL with JavaScript.

In this article, we’ll look at how to change the current URL with JavaScript.

How to change the current URL with JavaScript?

To change the current URL with JavaScript, we set the document.location.href property to the new URL.

For instance, we write

document.location.href = newUrl;

to set document.location.href to newUrl to navigate to the new URL.

Conclusion

To change the current URL with JavaScript, we set the document.location.href property to the new URL.

Categories
JavaScript Answers

How to write loops for promise with JavaScript?

Sometimes, we want to write loops for promise with JavaScript.

In this article, we’ll look at how to write loops for promise with JavaScript.

How to write loops for promise with JavaScript?

To write loops for promise with JavaScript, we can use the for-of loop with async and await.

For instance, we write

const sayHi = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("Hi");
      resolve();
    }, 3000);
  });
};

const asyncArray = [sayHi, sayHi, sayHi];

const hi = async () => {
  for (const func of asyncArray) {
    console.log(await func());
  }
};

to define the sayHi function that returns a promise.

Then we define the asyncArray array with references to sayHi.

Next, we have the hi function that loops through the functions in asyncArray and call them in the for-of loop.

We use await to get the resolve value of each promise.

Conclusion

To write loops for promise with JavaScript, we can use the for-of loop with async and await.

Categories
JavaScript Answers

How to add anything in the head element with JavaScript?

Sometimes, we want to add anything in the head element with JavaScript.

In this article, we’ll look at how to add anything in the head element with JavaScript.

How to add anything in the head element with JavaScript?

To add anything in the head element with JavaScript, we can use the document.head.appendChild method.

For instance, we write

const favicon = document.createElement("link");
favicon.id = "myFavicon";
favicon.rel = "shortcut icon";
favicon.href = "http://www.example.com/my-favicon.ico";

document.head.appendChild(favicon);

to create the link element with createElement.

Then we set the attributes of the link element with

favicon.id = "myFavicon";
favicon.rel = "shortcut icon";
favicon.href = "http://www.example.com/my-favicon.ico";

id, rel, and href set the attribute with the same name.

And then we call document.head.appendChild with favicon to insert it into the head element as its last child.

Conclusion

To add anything in the head element with JavaScript, we can use the document.head.appendChild method.

Categories
JavaScript Answers

How to insert space before capital letters with JavaScript?

Sometimes, we want to insert space before capital letters with JavaScript.

In this article, we’ll look at how to insert space before capital letters with JavaScript.

How to insert space before capital letters with JavaScript?

To insert space before capital letters with JavaScript, we can use the string replace method.

For instance, we write

const newS = s.replace(/([A-Z])/g, " $1").trim();

to call s.replace with the /([A-Z])/g to match all capital letters in string s.

The 2nd argument is ' $1' which means we add a space before the matched substring.

Finally, we call trim to trim the starting and ending whitespaces from the returned string.

Conclusion

To insert space before capital letters with JavaScript, we can use the string replace method.