Categories
JavaScript Answers

How show only button with file input?

Sometimes, we want to show only button with file input.

In this article, we’ll look at how to show only button with file input.

How show only button with file input?

To show only button with file input, we can hide the file input and call click on it when we click on the button.

For instance, we write

<input type="file" id="selectedFile" style="display: none" />
<input id="button" type="button" value="Browse..." />

to add the file input and the button.

We hide the file input with

style="display: none"

Then we can use it with

document.getElementById("button").onclick = () => {
  document.getElementById("selectedFile").click();
};

to select the button with

document.getElementById("button")

And then we set its onclick property to a function that clicks the file input with

document.getElementById("selectedFile").click();

to open it.

Conclusion

To show only button with file input, we can hide the file input and call click on it when we click on the button.

Categories
JavaScript Answers

How to check if a number is between two values in JavaScript?

Sometimes, we want to check if a number is between two values in JavaScript.

In this article, we’ll look at how to check if a number is between two values in JavaScript.

How to check if a number is between two values in JavaScript?

To check if a number is between two values in JavaScript, we can use comparison operators.

For instance, we write

if (windowSize > 100 && windowSize < 200) {
  // ...
}

to check if windowSize is between 100 and 200 exclusive with

windowSize > 100 && windowSize < 200

It returns true if windowSize is between 100 and 200 excluding the end numbers.

Conclusion

To check if a number is between two values in JavaScript, we can use comparison operators.

Categories
JavaScript Answers

How to change mock implementation on a per single test basis with Jest and JavaScript?

Sometimes, we want to change mock implementation on a per single test basis with Jest and JavaScript.

In this article, we’ll look at how to change mock implementation on a per single test basis with Jest and JavaScript.

How to change mock implementation on a per single test basis with Jest and JavaScript?

To change mock implementation on a per single test basis with Jest and JavaScript, we call mockImplementation.

For instance, we write

import { funcToMock } from "./somewhere";
jest.mock("./somewhere");

beforeEach(() => {
  funcToMock.mockImplementation(() => {
    //...
  });
});

test("test", () => {
  funcToMock.mockImplementation(() => {
    //...
  });
});

to call funcToMock.mockImplementation with a function that has the implementation of the funcToMock function we’re mocking.

Conclusion

To change mock implementation on a per single test basis with Jest and JavaScript, we call mockImplmentation.

Categories
JavaScript Answers

How to extract a string using JavaScript regex?

Sometimes, we want to extract a string using JavaScript regex.

In this article, we’ll look at how to extract a string using JavaScript regex.

How to extract a string using JavaScript regex?

To extract a string using JavaScript regex, we can use the regex exec method.

For instance, we write

const extractSummary = (iCalContent) => {
  const rx = /\nSUMMARY:(.*)\n/g;
  const arr = rx.exec(iCalContent);
  return arr[1];
};

to define the extractSummary function that gets the part of the iCalContent string that has the 'SUMMARY:' and some text after it.

Then we get the match with arr[1].

Conclusion

To extract a string using JavaScript regex, we can use the regex exec method.

Categories
JavaScript Answers

How to save JavaScript objects in sessionStorage?

Sometimes, we want to save JavaScript objects in sessionStorage.

In this article, we’ll look at how to save JavaScript objects in sessionStorage.

How to save JavaScript objects in sessionStorage?

To save JavaScript objects in sessionStorage, we convert the object to a string before saving it.

For instance, we write

const user = { name: "jane" };
sessionStorage.setItem("user", JSON.stringify(user));
const obj = JSON.parse(sessionStorage.user);

to call JSON.stringify with user to convert user to a JSON string.

Then we call setItem to save the string with key 'user'.

And then we can get the value and parse it back to an object with

JSON.parse(sessionStorage.user)

Conclusion

To save JavaScript objects in sessionStorage, we convert the object to a string before saving it.