Categories
JavaScript Answers

How to pass parameters in JavaScript onClick event?

To pass parameters in JavaScript onClick event, we set the onclick property of each element to its own click handler.

For instance, we write

for (let i = 0; i < 10; i++) {
  const link = document.createElement("a");
  link.setAttribute("href", "#");
  link.innerHTML = i.toString();
  link.onclick = () => {
    onClickLink(i.toString());
  };
  div.appendChild(link);
  div.appendChild(document.createElement("BR"));
}

to use a for loop to loop from 0 to 9.

In it, we create a link with createElement.

Then we call setAttribute to set the eleemnt’s href attribute value.

Next, we set its innerHTML property to the link text.

We then set its onclick property to the click event handler.

We set it to a function that calls onClickLink with i converted to a string.

Then we call appendChild to append the link to a div.

And we call it again to append a br element.

Categories
JavaScript Answers

How to get name of form element with JavaScript?

Sometimes, we want to get name of form element with JavaScript.

In this article, we’ll look at how to get name of form element with JavaScript.

How to get name of form element with JavaScript?

To get name of form element with JavaScript, we use the getAttribute method.

For instance, we write

const name = element.getAttribute("name");

to call element.getAttribute with 'name' to get the value of the name attribute of the element.

Conclusion

To get name of form element with JavaScript, we use the getAttribute method.

Categories
JavaScript Answers

How to detect a long touch pressure with JavaScript for Android and iPhone?

Sometimes, we want to detect a long touch pressure with JavaScript for Android and iPhone.

In this article, we’ll look at how to detect a long touch pressure with JavaScript for Android and iPhone.

How to detect a long touch pressure with JavaScript for Android and iPhone?

To detect a long touch pressure with JavaScript for Android and iPhone, we listen to the touchstart and touchend events.

For instance, we write

let touchStartTimeStamp = 0;
let touchEndTimeStamp = 0;

const onTouchStart = (e) => {
  touchStartTimeStamp = e.timeStamp;
};

const onTouchEnd = (e) => {
  touchEndTimeStamp = e.timeStamp;

  console.log(touchEndTimeStamp - touchStartTimeStamp);
};

window.addEventListener("touchstart", onTouchStart, false);
window.addEventListener("touchend", onTouchEnd, false);

to call addEventListener to listen for the touchstart and touchend events on window.

Then we get the timeStamp of when the touch starts from onTouchStart.

And we get the timeStamp of when the touch ends from onTouchEnd.

We also subtract touchEndTimeStamp by touchStartTimeStamp to get the touch duration in milliseconds.

Conclusion

To detect a long touch pressure with JavaScript for Android and iPhone, we listen to the touchstart and touchend events.

Categories
JavaScript Answers

How to get today’s date in Google Apps Script with JavaScript?

Sometimes, we want to get today’s date in Google Apps Script with JavaScript.

In this article, we’ll look at how to get today’s date in Google Apps Script with JavaScript.

How to get today’s date in Google Apps Script with JavaScript?

To get today’s date in Google Apps Script with JavaScript, we use the Utilities.formatDate method.

For instance, we write

const today = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");

to call the Utilities.formatDate to get today’s date in dd/MM/yyyy format with time zone GMT+1 as a string.

Conclusion

To get today’s date in Google Apps Script with JavaScript, we use the Utilities.formatDate method.

Categories
TypeScript Answers

How to add environment variables with dotenv and TypeScript?

Sometimes, we want to add environment variables with dotenv and TypeScript.

In this article, we’ll look at how to add environment variables with dotenv and TypeScript.

How to add environment variables with dotenv and TypeScript?

To add environment variables with dotenv and TypeScript, we call the dotenv.config method.

For instance, we write

import * as dotenv from "dotenv";
dotenv.config({ path: __dirname + "/.env" });

to call dotenv.config with an object with the path of the environment variable file to load the key-value pairs into process.env.

Conclusion

To add environment variables with dotenv and TypeScript, we call the dotenv.config method.