Categories
JavaScript Answers

How to remove element from array in forEach loop with JavaScript?

Sometimes, we want to remove element from array in forEach loop with JavaScript.

In this article, we’ll look at how to remove element from array in forEach loop with JavaScript.

How to remove element from array in forEach loop with JavaScript?

To remove element from array in forEach loop with JavaScript, we can use the array splice method.

For instance, we write

const review = ["a", "b", "c", "b", "a"];

review.forEach((item, index, arr) => {
  if (item === "a") {
    arr.splice(index, 1);
  }
});

to call review.forEach with a callback that checks if the item we’re iterating through is 'a'.

If it is, then we call arr.splice with index and 1 to remove the review entry at index.

Conclusion

To remove element from array in forEach loop with JavaScript, we can use the array splice method.

Categories
JavaScript Answers

How to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript?

Sometimes, we want to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript.

In this article, we’ll look at how to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript.

How to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript?

To fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript, we can select all the tooltip toggle elements and then create the tooltip with them.

For instance, we write

const tooltipTriggerList = [
  ...document.querySelectorAll('[data-bs-toggle="tooltip"]'),
];
const tooltipList = tooltipTriggerList.map((tooltipTriggerEl) => {
  return new bootstrap.Tooltip(tooltipTriggerEl, {
    trigger: "hover",
  });
});

to select all tooltip toggle elements with querySelectorAll.

And then we call map on them after spreading them to an array with a callback.

In the callback, we return the Tooltip that we create from each element, which is the tooltipTriggerEl.

And we set the trigger action for each tooltip to 'hover' to show the tooltip when we hover over the tooltip toggle element.

Conclusion

To fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript, we can select all the tooltip toggle elements and then create the tooltip with them.

Categories
JavaScript Answers

How to change cursor to waiting in JavaScript?

Sometimes, we want to change cursor to waiting in JavaScript.

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

How to change cursor to waiting in JavaScript?

To change cursor to waiting in JavaScript, we can set the cursor style.

For instance, we write

document.body.style.cursor = 'wait'

to change the cursor style to spinner.

And we set the cursor style back to the normal style with

document.body.style.cursor = 'default'

We set the style on the body element with both lines so we see the cursor no matter where the mouse is on the page.

Conclusion

To change cursor to waiting in JavaScript, we can set the cursor style.

Categories
JavaScript Answers

How to perform a str_replace in JavaScript, replacing text in JavaScript?

Sometimes, we want to perform a str_replace in JavaScript, replacing text in JavaScript.

In this article, we’ll look at how to perform a str_replace in JavaScript, replacing text in JavaScript.

How to perform a str_replace in JavaScript, replacing text in JavaScript?

To perform a str_replace in JavaScript, replacing text in JavaScript, we use the string replace method.

For instance, we write

text = text.replace("old", "new");

to replace the first instance of substring 'old' with 'new' in the text string.

We can also call replace with a regex as the first argument to match substring patterns in the string and do the replacemennt.

Conclusion

To perform a str_replace in JavaScript, replacing text in JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to parse a string with a comma thousand separator to a number with JavaScript?

Sometimes, we want to parse a string with a comma thousand separator to a number with JavaScript.

In this article, we’ll look at how to parse a string with a comma thousand separator to a number with JavaScript.

How to parse a string with a comma thousand separator to a number with JavaScript?

To parse a string with a comma thousand separator to a number with JavaScript, we remove all the commas from the string before we parse it.

For instance, we write

const output = parseFloat("2,299.00".replace(/,/g, ""));
console.log(output);

to return the "2,299.00" string without the commas with replace.

Then we call parseFloat to parse the returned string into a floating point number.

Conclusion

To parse a string with a comma thousand separator to a number with JavaScript, we remove all the commas from the string before we parse it.