Categories
JavaScript Answers

How to append array to FormData and send via Ajax with JavaScript?

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator.

For instance, we write

const formData = new FormData();
const arr = ["this", "is", "an", "array"];

for (const a of arr) {
  formData.append("arr[]", a);
}

console.log(...formData);

to loop through the arr array and call formData.append with the key and value of each FormData entry.

Then we use the spread operator to spread the formData key-value entries as arguments of console.log as arrays.

Conclusion

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to get all selected values from a multi-select element with JavaScript?

Sometimes, we want to get all selected values from a multi-select element with JavaScript.

In this article, we’ll look at how to get all selected values from a multi-select element with JavaScript.

How to get all selected values from a multi-select element with JavaScript?

To get all selected values from a multi-select element with JavaScript, we use the selectedOptions property.

For instance, we write

<select id="select-meal-type" multiple="multiple">
  <option value="1">Breakfast</option>
  <option value="2" selected>Lunch</option>
  <option value="3">Dinner</option>
  <option value="4" selected>Snacks</option>
  <option value="5">Dessert</option>
</select>

to add a multi-select box.

Then we write

const options = document.getElementById("select-meal-type").selectedOptions;
const values = Array.from(options).map(({ value }) => value);
console.log(values);

to get the select element by ID.

We get the selected options with selectedOptions.

Then we convert the selectedOptions into an array with Array.from.

And we call map with a callback to map the selected option objects to get the value from them.

The value has the value attribute value.

Conclusion

To get all selected values from a multi-select element with JavaScript, we use the selectedOptions property.

Categories
JavaScript Answers

How to listen to the form submit event in JavaScript?

Sometimes, we want to listen to the form submit event in JavaScript.

In this article, we’ll look at how to listen to the form submit event in JavaScript.

How to listen to the form submit event in JavaScript?

To listen to the form submit event in JavaScript, we call addEventListener on the form.

For instance, we write

document.querySelector("#myForm").addEventListener("submit", (e) => {
  if (!isValid) {
    e.preventDefault();
  }
});

to select the form with

document.querySelector("#myForm")

Then we call addEventListener on it with 'submit' and a callback that runs when we submit the form.

In the callback, we check if isValid is false.

If it is, we call e.preventDefault to prevent the default server side form submission behavior and do what we want.

Conclusion

To listen to the form submit event in JavaScript, we call addEventListener on the form.

Categories
JavaScript Answers

How to fix UnhandledPromiseRejectionWarning when testing using mocha or chai and JavaScript?

Sometimes, we want to fix UnhandledPromiseRejectionWarning when testing using mocha or chai and JavaScript.

In this article, we’ll look at how to fix UnhandledPromiseRejectionWarning when testing using mocha or chai and JavaScript.

How to fix UnhandledPromiseRejectionWarning when testing using mocha or chai and JavaScript?

To fix UnhandledPromiseRejectionWarning when testing using mocha or chai and JavaScript, we call catch to catch the rejected promise.

For instance, we write

it("should transition with the correct event", () => {
  //...
  return new Promise((resolve, reject) => {
    //...
  })
    .then((state) => {
      assert(state.action === "DONE", "should change state");
    })
    .catch((error) => {
      assert.isNotOk(error, "Promise error");
    });
});

to add a test with it.

We call catch to catch promise rejections from the promise.

And we call assert.isNotOK to check the error from the rejected promise.

Conclusion

To fix UnhandledPromiseRejectionWarning when testing using mocha or chai and JavaScript, we call catch to catch the rejected promise.

Categories
JavaScript Answers

How to add an event listener for when element becomes visible with JavaScript?

Sometimes, we want to add an event listener for when element becomes visible with JavaScript.

In this article, we’ll look at how to add an event listener for when element becomes visible with JavaScript.

How to add an event listener for when element becomes visible with JavaScript?

To add an event listener for when element becomes visible with JavaScript, we can use the IntersectionObserver constructor.

For instance, we write

const options = {
  root: document.documentElement,
};

const observer = new IntersectionObserver((entries, observer) => {
  entries.forEach((entry) => {
    callback(entry.intersectionRatio > 0);
  });
}, options);

observer.observe(element);

to create an IntersectionObserver object with a callback and the options object.

The callback is run when the element we’re watching changes.

We get the element that intersects the element we’re watching with entries.

We set options.root to the root level element that we’re watching for element visibility for.

Conclusion

To add an event listener for when element becomes visible with JavaScript, we can use the IntersectionObserver constructor.