Categories
JavaScript Answers

How to replace underscores with spaces with JavaScript?

Sometimes, we want to replace underscores with spaces with JavaScript.

In this article, we’ll look at how to replace underscores with spaces with JavaScript.

How to replace underscores with spaces with JavaScript?

To replace underscores with spaces with JavaScript, we use the string replace method.

For instance, we write

const s = str.replace(/_/g, " ");

to call str.replace with /_/g to match all underscores in str.

And we replace them all with ' ' as specified by the 2nd argument.

Then a new string with the replacements done is returned.

Conclusion

To replace underscores with spaces with JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to pass variable to HTML template in Nodemailer and JavaScript?

Sometimes, we want to pass variable to HTML template in Nodemailer and JavaScript.

In this article, we’ll look at how to pass variable to HTML template in Nodemailer and JavaScript.

How to pass variable to HTML template in Nodemailer and JavaScript?

To pass variable to HTML template in Nodemailer and JavaScript, we put them in the context object property.

For instance, we write

const mailOptions = {
  from: "abc@example.com",
  to: "username@example.com",
  subject: "Sending email",
  template: "yourTemplate",
  context: {
    username,
    whatever,
  },
};

to put username and whatever into context.

Then in yourTemplate, we write

{{ username }}

to render the username value.

Conclusion

To pass variable to HTML template in Nodemailer and JavaScript, we put them in the context object property.

Categories
JavaScript Answers

How to listen for DOM ready event with JavaScript?

Sometimes, we want to listen for DOM ready event with JavaScript.

In this article, we’ll look at how to listen for DOM ready event with JavaScript.

How to listen for DOM ready event with JavaScript?

To listen for DOM ready event with JavaScript, we listen to the DOMContentLoaded event.

For instance, we write

const onReady = () => {
  /* ... */
};

document.addEventListener("DOMContentLoaded", onReady);

to call addEventListener to listen for the DOMContentLoaded event.

We set onReady as its event listener.

It’s called when the DOM is done loading.

Conclusion

To listen for DOM ready event with JavaScript, we listen to the DOMContentLoaded event.

Categories
JavaScript Answers

How to get the second digit from a number with JavaScript?

Sometimes, we want to get the second digit from a number with JavaScript.

In this article, we’ll look at how to get the second digit from a number with JavaScript.

How to get the second digit from a number with JavaScript?

To get the second digit from a number with JavaScript, we convert it to a string and destructure it.

For instance, we write

const [, digit] = myVar.toString();

to call myVar.toString to return a string version of the myVar number.

Then we get the 2nd digit with `destructuring.

Conclusion

To get the second digit from a number with JavaScript, we convert it to a string and destructure it.

Categories
JavaScript Answers

How to change onclick attribute with JavaScript?

To change onclick attribute with JavaScript, we set the onclick property.

For instance, we write

document.getElementById(id).onclick = () => {
  //...
};

to get the element with getElementById.

Then we set its onclick property to the click handler to set the onclick attribute value.