Categories
JavaScript Answers

How to add a listener which is called if less than 2 options on a select element with JavaScript?

Sometimes, we want to add a listener which is called if less than 2 options on a select element with JavaScript.

In this article, we’ll look at how to add a listener which is called if less than 2 options on a select element with JavaScript.

How to add a listener which is called if less than 2 options on a select element with JavaScript?

To add a listener which is called if less than 2 options on a select element with JavaScript, we listen to the click event.

For instance, we write

<select id="activitySelector">
  <option value="addNew">Add New Item</option>
</select>

to add the select element.

Then we write

const activities = document.getElementById("activitySelector");

const addActivityItem = () => {
  //...
};

activities.addEventListener("click", () => {
  const options = activities.querySelectorAll("option");
  const count = options.length;
  if (typeof count === "undefined" || count < 2) {
    addActivityItem();
  }
});

to select the select element.

And then we call addEventListener to add a click event listener to the drop down.

In it, we check if there’re less than 2 options with

const options = activities.querySelectorAll("option");
const count = options.length;

and count < 2.

We get all the options in the drop down with activities.querySelectorAll("option").

If count is undefined or if it’s less than 2, then we call the addActivityItem function.

Conclusion

To add a listener which is called if less than 2 options on a select element with JavaScript, we listen to the click event.

Categories
JavaScript Answers

How to return a variable from the child promise with JavaScript?

Sometimes, we want to return a variable from the child promise with JavaScript.

In this article, we’ll look at how to return a variable from the child promise with JavaScript.

How to return a variable from the child promise with JavaScript?

To return a variable from the child promise with JavaScript, we use async and await.

For instance, we write

const top = async () => {
  const parent = await ParentPromise();
  const child = await ChildPromise(parent.id);
  return child.result.items;
};

to define the top function.

In it, we get the result of the ParentPromise promise with await.

And then we get the result of the ChildPromise promise with await.

Finally, we return the child.result.items property.

We can then use

const items = await top();

to get the value in another async function.

Conclusion

To return a variable from the child promise with JavaScript, we use async and await.

Categories
JavaScript Answers

How to zoom with HTML5 Canvas and JavaScript?

Sometimes, we want to zoom with HTML5 Canvas and JavaScript.

In this article, we’ll look at how to zoom with HTML5 Canvas and JavaScript.

How to zoom with HTML5 Canvas and JavaScript?

To zoom with HTML5 Canvas and JavaScript, we call drawImage with different image dimensions.

For instance, we draw the initial image with

ctx.drawImage(
  img,
  0,
  0,
  img.width,
  img.height,
  0,
  0,
  canvas.width,
  canvas.height
);

where img is the image element with the image.

The 2nd and 3rd arguments are the x and y coordinates of the top left corner.

The next 2 arguments are the image width and height.

Then when we zoom, we call drawImage by writing

ctx.drawImage(
  img,
  img.width / 4,
  img.height / 4,
  img.width / 2,
  img.height / 2,
  0,
  0,
  canvas.width,
  canvas.height
);

to change the top left corner coordinate values and the image’s size.

Conclusion

To zoom with HTML5 Canvas and JavaScript, we call drawImage with different image dimensions.

Categories
JavaScript Answers

How to pass JavaScript variable to link?

Sometimes, we want to pass JavaScript variable to link.

In this article, we’ll look at how to pass JavaScript variable to link.

How to pass JavaScript variable to link?

To pass JavaScript variable to link, we use the setAttribute method.

For instance, we write

<a id="link2"> ... </a>

to add a link.

Then we write

const strLink = "2.html";
document.getElementById("link2").setAttribute("href", strLink);

to select the link with getElementById.

And then we call setAttribute with 'href' and the value of the href attribute to set the value of the href attribute.

Conclusion

To pass JavaScript variable to link, we use the setAttribute method.

Categories
JavaScript Answers

How to add mailto link using JavaScript?

Sometimes, we want to add mailto link using JavaScript.

In this article, we’ll look at how to add mailto link using JavaScript.

How to add mailto link using JavaScript?

To add mailto link using JavaScript, we set the window.location.href value.

For instance, we write

window.location.href = "mailto:mail@example.org";

to set window.location.href to "mailto:mail@example.org" to open the default email client with the to field set to mail@example.org.

Conclusion

To add mailto link using JavaScript, we set the window.location.href value.