Categories
JavaScript Answers

How to detect shift+tab press with JavaScript?

Sometimes, we want to detect shift+tab press with JavaScript.

In this article, we’ll look at how to detect shift+tab press with JavaScript.

How to detect shift+tab press with JavaScript?

To detect shift+tab press with JavaScript, we can check if the shiftKey is true and the keyCode is 9.

For instance, we write

if (event.shiftKey && event.keyCode === 9) {
  // ...
}

to check if the shiftKey is pressed and while tab is pressed.

The keyCode for the tab key is 9.

event is the keyboard event object we get from the keyboard event handler callback’s parameter.

Conclusion

To detect shift+tab press with JavaScript, we can check if the shiftKey is true and the keyCode is 9.

Categories
JavaScript Answers

How to turn TypeScript object into JSON string?

Sometimes, we want to turn TypeScript object into JSON string.

In this article, we’ll look at how to turn TypeScript object into JSON string.

How to turn TypeScript object into JSON string?

To turn TypeScript object into JSON string, we can use the JSON.stringify method.

For instance, we write

const jsObj = { employee: { name: "John", age: 30, city: "New York" } };
const jsonString = JSON.stringify(jsObj);
console.log(jsonString);

to call JSON.stringify with the jsObj object to convert it to a JSON string.

Conclusion

To turn TypeScript object into JSON string, we can use the JSON.stringify method.

Categories
JavaScript Answers

How to change placeholder text with JavaScript?

Sometimes, we want to change placeholder text with JavaScript.

In this article, we’ll look at how to change placeholder text with JavaScript.

How to change placeholder text with JavaScript?

To change placeholder text with JavaScript, we can set the placeholder property.

For instance, we write

document.getElementsByName("Email")[0].placeholder = "new text for email";

to select the input with getElementsByName.

Then we get the first element with [0].

And then we set the placeholder property to the placeholder text we want to show.

Conclusion

To change placeholder text with JavaScript, we can set the placeholder property.

Categories
JavaScript Answers

How to convert Unix epoch time to date in Google Sheets and JavaScript?

Sometimes, we want to convert Unix epoch time to date in Google Sheets and JavaScript.

In this article, we’ll look at how to convert Unix epoch time to date in Google Sheets and JavaScript.

How to convert Unix epoch time to date in Google Sheets and JavaScript?

To convert Unix epoch time to date in Google Sheets and JavaScript, we can add a JavaScript function that does the conversion.

We open the script editor with Tools > Script Editor… >

Then we add

function millisToDate(timeInMillis) {
  const yourDateFromMillis = new Date(timeInMillis);
  return yourDateFromMillis;
}

to define the millisToDate function.

In it, we create a Date object from the timeInMillis parameter.

timeInMillis is the Unix timestamp in milliseconds.

Then we return the Date object.

Conclusion

To convert Unix epoch time to date in Google Sheets and JavaScript, we can add a JavaScript function that does the conversion.

Categories
JavaScript Answers

How to get JavaScript window.location.href without the hash?

Sometimes, we want to get JavaScript window.location.href without the hash.

In this article, we’ll look at how to get JavaScript window.location.href without the hash.

How to get JavaScript window.location.href without the hash?

To get JavaScript window.location.href without the hash, we can use the string split method.

For instance, we write

const [uri] = window.location.href.split("#");

to call window.location.href.split withh '#' and destructure the first part to get the URL without the hash part and assign it to uri.

Conclusion

To get JavaScript window.location.href without the hash, we can use the string split method.