Categories
JavaScript Answers

How to import module from URL with JavaScript?

Sometimes, we want to import module from URL with JavaScript.

In this article, we’ll look at how to import module from URL with JavaScript.

How to import module from URL with JavaScript?

To import module from URL with JavaScript, we use import with a URL.

For instance, we write

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>Hello World</title>
    <script type="module" src="./hello.js"></script>
  </head>
  <body></body>
</html>

to load the hello.js module with a script tag.

Then we write

import ip6 from "https://cdn.jsdelivr.net/gh/elgs/ip6/ip6.js";

to import the module located at https://cdn.jsdelivr.net/gh/elgs/ip6/ip6.js in hello.js.

Conclusion

To import module from URL with JavaScript, we use import with a URL.

Categories
JavaScript Answers

How to get x and y coordinates on mouse click with JavaScript?

Sometimes, we want to get x and y coordinates on mouse click with JavaScript.

In this article, we’ll look at how to get x and y coordinates on mouse click with JavaScript.

How to get x and y coordinates on mouse click with JavaScript?

To get x and y coordinates on mouse click with JavaScript, we can get the clientX and clientY properties from the mouse event object.

For instance, we write

const printMousePos = (event) => {
  const { clientX, clientY } = event;
  document.body.textContent = `${clientX}, ${clientY}`;
};

document.addEventListener("click", printMousePos);

to call addEventListener to add a click listener to the page.

printMousePos is called when we click on anything on the page.

Then we get the x and y coordinates of the mouse with clientX and clientY respectively.

Conclusion

To get x and y coordinates on mouse click with JavaScript, we can get the clientX and clientY properties from the mouse event object.

Categories
JavaScript Answers

How to fix JavaScript (+) sign concatenating instead of giving sum of variables?

Sometimes, we want to fix JavaScript (+) sign concatenating instead of giving sum of variables.

In this article, we’ll look at how to fix JavaScript (+) sign concatenating instead of giving sum of variables.

How to fix JavaScript (+) sign concatenating instead of giving sum of variables?

To fix JavaScript (+) sign concatenating instead of giving sum of variables, we should put parentheses around the expression that we want to add instead of concatenate.

For instance, we write

const divID = "question-" + (i + 1);

to wrap i + 1 with parentheses to return their sum instead of concatenating them.

Conclusion

To fix JavaScript (+) sign concatenating instead of giving sum of variables, we should put parentheses around the expression that we want to add instead of concatenate.

Categories
JavaScript Answers

How to change button text on click with JavaScript?

Sometimes, we want to change button text on click with JavaScript.

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

How to change button text on click with JavaScript?

To change button text on click with JavaScript, we can add a click listener to the button.

For instance, we write

<input id="curtainInput" type="button" value="Open Curtain" />

to add a button input.

Then we write

document.getElementById("curtainInput").addEventListener(
  "click",
  (event) => {
    if (event.target.value === "Open Curtain") {
      event.target.value = "Close Curtain";
    } else {
      event.target.value = "Open Curtain";
    }
  },
  false
);

to select the button with getElementById.

Then we add a click listener to it with addEventListener.

In it, we check if the value attribute is 'Open Curtain'.

If it is, we set it to 'Close Curtain'.

Otherwise, we set it to 'Open Curtain'.

Conclusion

To change button text on click with JavaScript, we can add a click listener to the button.

Categories
JavaScript Answers

How to capture a backspace on the keydown event with JavaScript?

Sometimes, we want to capture a backspace on the keydown event with JavaScript.

In this article, we’ll look at how to capture a backspace on the keydown event with JavaScript.

How to capture a backspace on the keydown event with JavaScript?

To capture a backspace on the keydown event with JavaScript, we can check the keyCode property.

For instance, we write

document.getElementById("foo").addEventListener("keydown", (event) => {
  if (event.keyCode === 8) {
    console.log("BACKSPACE was pressed");
    event.preventDefault();
  }
  if (event.keyCode === 46) {
    console.log("DELETE was pressed");
    event.preventDefault();
  }
});

to call addEventListener to listen to the keydown event.

In the event listener, we get the keyCode property.

If it’s 8, then backspace key is pressed.

And if it’s 46, the delete key is pressed.

Conclusion

To capture a backspace on the keydown event with JavaScript, we can check the keyCode property.