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
JavaScript Answers

How to add anchor tags dynamically to a div in JavaScript?

To add anchor tags dynamically to a div in JavaScript, we use the createElement method.

For instance, we write

const myDiv = document.getElementById("myDiv");
const aTag = document.createElement("a");
aTag.setAttribute("href", "yourlink.html");
aTag.innerText = "link text";
myDiv.appendChild(aTag);

to select the div with getElementById.

Then we create a link with createElement.

Next, we call setAttribute to set the value of the href attribute.

We set the innerText property to set the link text.

Finally, we add the link as the last child of the div with myDiv.appendChild.

Categories
JavaScript Answers

How to resize image with JavaScript?

Sometimes, we want to resize image with JavaScript.

In this article, we’ll look at how to resize image with JavaScript.

How to resize image with JavaScript?

To resize image with JavaScript, we set the width and height properties.

For instance, we write

image.style.width = "50%";
image.style.height = "auto";

to set the style.width and style.height properties to set the width and height of the image.

Conclusion

To resize image with JavaScript, we set the width and height properties.