Categories
JavaScript Answers

How to trigger event with parameters with JavaScript?

Sometimes, we want to trigger event with parameters with JavaScript.

In this article, we’ll look at how to trigger event with parameters with JavaScript.

How to trigger event with parameters with JavaScript?

To trigger event with parameters with JavaScript, we can trigger a custom event.

For instance, we write:

<button>
  click me
</button>

to add a button.

Then we write:

const button = document.querySelector("button");
button.addEventListener("custom-event", (e) => {
  console.log("custom-event", e.detail);
});
button.onclick = (e) => {
  const event = new CustomEvent("custom-event", {
    'detail': {
      customInfo: 10,
      customProp: 20
    }
  });
  e.target.dispatchEvent(event);
};

to select the button with querySelector.

Then we call button.addEventListener to add an event listener for the custom-event event.

We get the event data from e.detail.

Then we add a click listener to the button by setting button.onclick to a function that creates a new CustomEvent instance with the event name and some event data.

We then call e.target.dispatchEvent with event to trigger the custom-event event on the button.

Therefore, we see {customInfo: 10, customProp: 20} logged when we click the button.

Conclusion

To trigger event with parameters with JavaScript, we can trigger a custom event.

Categories
JavaScript Answers

How to change cursor when loading page in JavaScript?

Sometimes, we want to change cursor when loading page in JavaScript

In this article, we’ll look at how to change cursor when loading page in JavaScript.

How to change cursor when loading page in JavaScript?

To change cursor when loading page in JavaScript, we can set the cursor CSS property of the body element.

For instance, we write:

document.body.style.cursor = 'wait';
setTimeout(() => {
  document.body.style.cursor = 'default';
}, 2000)

We set document.body.style.cursor to 'wait' to show the loading cursor.

Then we set document.body.style.cursor to 'default' to show the default arrow cursor after 2 seconds.

Conclusion

To change cursor when loading page in JavaScript, we can set the cursor CSS property of the body element.

Categories
JavaScript Answers

How to create a canvas 2d context without displaying the canvas with JavaScript?

Sometimes, we want to create a canvas 2d context without displaying the canvas with JavaScript.

In this article, we’ll look at how to create a canvas 2d context without displaying the canvas with JavaScript.

How to create a canvas 2d context without displaying the canvas with JavaScript?

To create a canvas 2d context without displaying the canvas with JavaScript, we can use the document.createElement method.

For instance, we write:

const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 100;

const ctx = canvas.getContext('2d');
ctx.fillStyle = '#f00';
ctx.fillRect(20, 10, 80, 50);

const imgUrl = canvas.toDataURL();
const img = document.createElement('img');
img.src = imgUrl

document.body.appendChild(img)

to create the canvas with createElement.

Then we get the context with getContext.

Next, we set the fill style and add a rectangle with fillStyle and fillRect.

Then we convert the canvas to an image URL string with toDataURL.

Finally, we create an img element with createElement, set the src attribute to imgUrl and append the img element as a child of the body element with document.body.appendChild.

Now we should see a red rectangle displayed,

Conclusion

To create a canvas 2d context without displaying the canvas with JavaScript, we can use the document.createElement method.

Categories
JavaScript Answers

How to convert enums to array of values with JavaScript?

Sometimes, we want to convert enums to array of values with JavaScript.

In this article, we’ll look at how to convert enums to array of values with JavaScript.

How to convert enums to array of values with JavaScript?

To convert enums to array of values with JavaScript, we can use the Object.values method.

For instance, we write:

const types = {
  "WHITE": 0,
  "BLACK": 1
}
const vals = Object.values(types)
console.log(vals)

to call Object.values with types to return an array of the property values.

Therefore, vals is [0, 1].

Conclusion

To convert enums to array of values with JavaScript, we can use the Object.values method.

Categories
JavaScript Answers

How to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour?

Sometimes, we want to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour.

In this article, we’ll look at how to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour.

How to change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour?

To change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour, we can use the % operator.

For instance, we write:

const hours12 = (date) => {
  return (date.getHours() + 24) % 12 || 12;
}

const d = new Date('December 25, 2022 23:15:30')
console.log(hours12(d))

We define the hours12 to return the hours of the date in 12 hour format instead of 24.

In the function, we call date.getHours to get the hours, which is between 1 and 24.

Then we convert it to 12 hour format by adding 24 and then use the % to get the remainder when the result is divided by 12.

If the remainder is 0, then we return 12.

Therefore, the console log should log 11.

Conclusion

To change the 1-24 hour return from the JavaScript date’s getHours method to a 1-12 hour, we can use the % operator.