Categories
JavaScript Answers

How to add a checkbox check event listener with JavaScript?

Sometimes, we want to add a checkbox check event listener with JavaScript.

In this article, we’ll look at how to add a checkbox check event listener with JavaScript.

How to add a checkbox check event listener with JavaScript?

To add a checkbox check event listener with JavaScript, we can listen for the change event.

For instance, we write

<input type="checkbox" name="checkbox" />

to add a checkbox.

Then we write

const checkbox = document.querySelector("input[name=checkbox]");

checkbox.addEventListener("change", (e) => {
  if (e.target.checked) {
    console.log("Checkbox is checked..");
  } else {
    console.log("Checkbox is not checked..");
  }
});

to select the checkbox with querySelector.

Then we call checkbox.addEventListener to add an event listener for the change event.

We get the checked value of the checkbox with e.target.checked.

If it’s true, it’s checked.

Otherwise, it’s not.

Conclusion

To add a checkbox check event listener with JavaScript, we can listen for the change event.

Categories
JavaScript Answers

How to copy or put text on the clipboard with JavaScript?

Sometimes, we want to copy or put text on the clipboard with JavaScript.

In this article, we’ll look at how to copy or put text on the clipboard with JavaScript.

How to copy or put text on the clipboard with JavaScript?

To copy or put text on the clipboard with JavaScript, we can use the document.execCommand method.

For instance, we write

const copy = () => {
  document.getElementById("myText").select();
  document.execCommand("copy");
};

to create copy function that selects the input element with ID myText with getElementById.

Then we call select on it to select the input text.

Next we call document.execCommand with 'copy' to copy the selected text into the clipboard.

Conclusion

To copy or put text on the clipboard with JavaScript, we can use the document.execCommand method.

Categories
JavaScript Answers

How to detect online or offline event cross-browser with JavaScript?

Sometimes, we want to detect online or offline event cross-browser with JavaScript.

In this article, we’ll look at how to detect online or offline event cross-browser with JavaScript.

How to detect online or offline event cross-browser with JavaScript?

To detect online or offline event cross-browser with JavaScript, we can use the navigator.onLine property.

For instance, we write

if (navigator.onLine) {
  // online
} else {
  // offline
}

to check the navigator.onLine value with the if statement.

If navigator.onLine is true, then the device is online.

Otherwise, it’s offline.

Conclusion

To detect online or offline event cross-browser with JavaScript, we can use the navigator.onLine property.

Categories
JavaScript Answers

How to mock an exported constant in Jest and JavaScript?

Sometimes, we want to mock an exported constant in Jest and JavaScript.

In this article, we’ll look at how to mock an exported constant in Jest and JavaScript.

How to mock an exported constant in Jest and JavaScript?

To mock an exported constant in Jest and JavaScript, we can mock the imported modules with jest.mock.

For instance, we write

const mockTrue = { ENABLED: true };
const mockFalse = { ENABLED: false };

describe("allowThrough", () => {
  beforeEach(() => {
    jest.resetModules();
  });

  test("success", () => {
    jest.mock("./constants", () => mockTrue);
    const { ENABLED } = require("./constants");
    const { allowThrough } = require("./allowThrough");

    expect(ENABLED).toBe(true);
    expect(allowThrough({ value: 1 })).toBe(true);
  });

  test("fail, ENABLED === false", () => {
    jest.mock("./constants", () => mockFalse);
    const { ENABLED } = require("./constants");
    const { allowThrough } = require("./allowThrough");

    expect(ENABLED).toBe(false);
    expect(allowThrough({ value: 1 })).toBe(false);
  });
});

to call jest.mock in each test with the module path and a function that returns the content of the module.

Then we call expect to check for the result we’re looking for in each test.

In the beforeEach callback, we call jest.resetModules to reset the modules to their original values.

Conclusion

To mock an exported constant in Jest and JavaScript, we can mock the imported modules with jest.mock.

Categories
JavaScript Answers

How to detect backspace and del on input event with JavaScript?

Sometimes, we want to detect backspace and del on input event with JavaScript.

In this article, we’ll look at how to detect backspace and del on input event with JavaScript.

How to detect backspace and del on input event with JavaScript?

To detect backspace and del on input event with JavaScript, we can check the keydown event object’s key property.

For instance, we write

input.addEventListener("keydown", ({ key }) => {
  if (["Backspace", "Delete"].includes(key)) {
    return false;
  }
});

to add a keydown event listener to the input with input.addEventListener.

In the event listener callback, we check if the key property is 'Backspace' or 'Delete'.

If it is, then we return false to stop the default action.

Conclusion

To detect backspace and del on input event with JavaScript, we can check the keydown event object’s key property.