Categories
JavaScript Answers

How to convert binary representation of number from string to integer number in JavaScript?

Sometimes, we want to convert binary representation of number from string to integer number in JavaScript.

In this article, we’ll look at how to convert binary representation of number from string to integer number in JavaScript.

How to convert binary representation of number from string to integer number in JavaScript?

To convert binary representation of number from string to integer number in JavaScript, we use the parseInt function.

For instance, we write

const a = parseInt("01001011", 2);

to call parseInt with a binary string and 2 to return the decimal value of the binary string.

The 2nd argument is the radix.

Conclusion

To convert binary representation of number from string to integer number in JavaScript, we use the parseInt function.

Categories
JavaScript Answers

How to clear or replace a cached image with JavaScript?

Sometimes, we want to clear or replace a cached image with JavaScript.

In this article, we’ll look at how to clear or replace a cached image with JavaScript.

How to clear or replace a cached image with JavaScript?

To clear or replace a cached image with JavaScript, we call fetch.

For instance, we write

fetch("/thing/stuck/in/cache", { method: "POST", credentials: "include" });

to call fetch with the URL of image stuck in the cache by making a post request to it.

Then the image will be reloaded.

Conclusion

To clear or replace a cached image with JavaScript, we call fetch.

Categories
JavaScript Answers

How to add an onchange event to a select box via JavaScript?

Sometimes, we want to add an onchange event to a select box via JavaScript.

In this article, we’ll look at how to add an onchange event to a select box via JavaScript.

How to add an onchange event to a select box via JavaScript?

To add an onchange event to a select box via JavaScript, we call addEventListener.

For instance, we write

select.addEventListener(
  "change",
  (e) => {
    toggleSelect(e.target.id);
  },
  false
);

to call addEventListener to add a change event listener to the select element.

We set the event listener to the function that calls the toggleSelect function with the element’s ID.

e.target.id has the element’s ID.

Conclusion

To add an onchange event to a select box via JavaScript, we call addEventListener.

Categories
JavaScript Answers

How to implement swipe gestures for mobile devices with JavaScript?

Sometimes, we want to implement swipe gestures for mobile devices with JavaScript.

In this article, we’ll look at how to implement swipe gestures for mobile devices with JavaScript.

How to implement swipe gestures for mobile devices with JavaScript?

To implement swipe gestures for mobile devices with JavaScript, we use hammerjs.

To add it, we add a script tag with https://hammerjs.github.io/dist/hammer.min.js as the src attribute value.

Then we write

const hammertime = new Hammer(myElement, myOptions);
hammertime.on("swipe", (ev) => {
  console.log(ev);
});

to create a Hammer object with the myElement that we want to listen to events for and then call on to listen to the 'swipe' event.

Conclusion

To implement swipe gestures for mobile devices with JavaScript, we use hammerjs.

Categories
JavaScript Answers

How to convert x,y coordinates to an angle with JavaScript?

Sometimes, we want to convert x,y coordinates to an angle with JavaScript.

In this article, we’ll look at how to convert x,y coordinates to an angle with JavaScript.

How to convert x,y coordinates to an angle with JavaScript?

To convert x,y coordinates to an angle with JavaScript, we use the Math.atan2 method.

For instance, we write

const deltaX = x2 - x1;
const deltaY = y2 - y1;
const rad = Math.atan2(deltaY, deltaX);
const deg = rad * (180 / Math.PI);

to get deltaX and deltaY by subtracting the x and y coordinates.

Then we call Math.atan2 with deltaY and deltaX to get the angle in radians.

Then we convert it to degrees by multiplying that by 180 / Math.PI.

Conclusion

To convert x,y coordinates to an angle with JavaScript, we use the Math.atan2 method.