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.

Categories
JavaScript Answers

How to convert inline SVG to Base64 string with JavaScript?

Sometimes, we want to convert inline SVG to Base64 string with JavaScript.

In this article, we’ll look at how to convert inline SVG to Base64 string with JavaScript.

How to convert inline SVG to Base64 string with JavaScript?

To convert inline SVG to Base64 string with JavaScript, we use the serializeToString method.

For instance, we write

const s = new XMLSerializer().serializeToString(document.getElementById("svg"));
const encodedData = window.btoa(s);

to call serializeToString with the svg element we get from getElementById to convert it to an svg string.

Then we call btoa with s to convert it to a base64 URL string.

Conclusion

To convert inline SVG to Base64 string with JavaScript, we use the serializeToString method.

Categories
JavaScript Answers

How to remove duplicate objects from JSON array with JavaScript?

Sometimes, we want to remove duplicate objects from JSON array with JavaScript.

In this article, we’ll look at how to remove duplicate objects from JSON array with JavaScript.

How to remove duplicate objects from JSON array with JavaScript?

To remove duplicate objects from JSON array with JavaScript, we can use a set.

For instance, we write

const data = [
  { Grade: "Math K", Domain: "Counting & Cardinality" },
  { Grade: "Math K", Domain: "Geometry" },
  { Grade: "Math 1", Domain: "Counting & Cardinality" },
  { Grade: "Math 1", Domain: "Orders of Operation" },
  { Grade: "Math 2", Domain: "Geometry" },
];
const set = new Set(data.map((item) => JSON.stringify(item)));
const dedup = [...set].map((item) => JSON.parse(item));

to call data.map to map each object in data to JSON strings with JSON.stringify.

Then we put them all in a set with the Set constructor.

Next, we spread the set back to an array then call map to map them back to objects with JSON.parse.

Conclusion

To remove duplicate objects from JSON array with JavaScript, we can use a set.