Categories
JavaScript Answers

How to merge image using JavaScript?

Sometimes, we want to merge image using JavaScript.

In this article, we’ll look at how to merge image using JavaScript.

How to merge image using JavaScript?

To merge image using JavaScript, we use the merge-images package.

To install it, we run

npm i merge-images

Then we use it by writing

import mergeImages from "merge-images";

const b64 = await mergeImages(["/body.png", "/eyes.png", "/mouth.png"]);
document.querySelector("img").src = b64;

to call mergeImages to return a promise that merges the images at the paths in the array.

Then we get the b64 base64 image URL string from the returned promise and assign that as the value of the src attribute of an img element.

Conclusion

To merge image using JavaScript, we use the merge-images package.

Categories
JavaScript Answers

How to get the referrer with JavaScript?

Sometimes, we want to get the referrer with JavaScript.

In this article, we’ll look at how to get the referrer with JavaScript.

How to get the referrer with JavaScript?

To get the referrer with JavaScript, we use the document.referrer property.

For instance, we write

const referrer = document.referrer;

to get the referrer of the current page.

Conclusion

To get the referrer with JavaScript, we use the document.referrer property.

Categories
JavaScript Answers

How to set volume of audio element with JavaScript?

Sometimes, we want to set volume of audio element with JavaScript.

In this article, we’ll look at how to set volume of audio element with JavaScript.

How to set volume of audio element with JavaScript?

To set volume of audio element with JavaScript, we set its volume property.

For instance, we write

const audio = new Audio("test.wav");
audio.volume = 0.2;

to create an Audio object.

Then we set its volume property to set the volume level.

Conclusion

To set volume of audio element with JavaScript, we set its volume property.

Categories
JavaScript Answers

How to get all elements which name starts with some string with JavaScript?

Sometimes, we want to get all elements which name starts with some string with JavaScript.

In this article, we’ll look at how to get all elements which name starts with some string with JavaScript.

How to get all elements which name starts with some string with JavaScript?

To get all elements which name starts with some string with JavaScript, we call querySelectorAll.

For instance, we write

const els = document.querySelectorAll("[name^=q1_]");

to call querySelectorAll to select all elements with the name attribute starting with q1_ in document.

Conclusion

To get all elements which name starts with some string with JavaScript, we call querySelectorAll.

Categories
JavaScript Answers

How to pass parameters in JavaScript onClick event?

To pass parameters in JavaScript onClick event, we set the onclick property of each element to its own click handler.

For instance, we write

for (let i = 0; i < 10; i++) {
  const link = document.createElement("a");
  link.setAttribute("href", "#");
  link.innerHTML = i.toString();
  link.onclick = () => {
    onClickLink(i.toString());
  };
  div.appendChild(link);
  div.appendChild(document.createElement("BR"));
}

to use a for loop to loop from 0 to 9.

In it, we create a link with createElement.

Then we call setAttribute to set the eleemnt’s href attribute value.

Next, we set its innerHTML property to the link text.

We then set its onclick property to the click event handler.

We set it to a function that calls onClickLink with i converted to a string.

Then we call appendChild to append the link to a div.

And we call it again to append a br element.