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 check for multiple string matches with JavaScript?

Sometimes, we want to check for multiple string matches with JavaScript.

In this article, we’ll look at how to check for multiple string matches with JavaScript.

How to check for multiple string matches with JavaScript?

To check for multiple string matches with JavaScript, we can use the JavaScript string’s match method with a regex.

For instance, we write:

const str = 'audio video'
if (str.match(/(video|audio)/)) {
  console.log('found')
} else {
  console.log('not found')
}

to call str.match with a regex.

Since 'audio' and 'video' are both in str, str.match(/(video|audio)/) returns true.

Conclusion

To check for multiple string matches with JavaScript, we can use the JavaScript string’s match method with a regex.

Categories
JavaScript Answers

How to wait until setInterval is done with JavaScript?

Sometimes, we want to wait until setInterval is done with JavaScript.

In this article, we’ll look at how to wait until setInterval is done with JavaScript.

How to wait until setInterval is done with JavaScript?

To wait until setInterval is done with JavaScript, we can call setInterval in a promise.

For instance, we write:

const rollDice = () => {
  return new Promise((resolve, reject) => {
    let numberOfRollsLeft = 5
    const intervalId = setInterval(() => {
      const diceValue = Math.floor(Math.random() * 6 + 1);
      numberOfRollsLeft--
      if (numberOfRollsLeft === 0) {
        clearInterval(intervalId);
        resolve(diceValue);
      }
    }, 50);
  });
}

const roll = async () => {
  const val = await rollDice()
  console.log(val)
}
roll()

We define rollDice which returns a promise.

We create the promise with the Promise constructor with a callback that calls setInterval with a callback to draw a random number.

If numberOfRollsLeft is 0, then we call clearInterval to stop the timer and call resolve with the diceValue to return that as the resolved value.

Next, we call rollDice in the roll function and use await to get the resolved promise value.

Conclusion

To wait until setInterval is done with JavaScript, we can call setInterval in a promise.

Categories
JavaScript Answers

How to filter strings in array based on content with JavaScript?

Sometimes, we want to filter strings in array based on content with JavaScript.

In this article, we’ll look at how to filter strings in array based on content with JavaScript.

How to filter strings in array based on content with JavaScript?

To filter strings in array based on content with JavaScript, we can use the JavaScript array’s filter instance method.

For instance, we write:

const myArray = ["bedroomone", "bedroomonetwo", "bathroom"];
const pattern = /bedroom/;
const filtered = myArray.filter((str) => {
  return pattern.test(str);
});
console.log(filtered)

We call myArray.filter with a callback that matches all the strings that has 'bedroom' in it by calling pattern.test with str.

Therefore, filtered is ['bedroomone', 'bedroomonetwo'].

Conclusion

To filter strings in array based on content with JavaScript, we can use the JavaScript array’s filter instance method.