Categories
JavaScript Answers

How to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript?

Sometimes, we want to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript.

In this article, we’ll look at how to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript.

How to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript?

To fix the fetch Missing boundary in multipart/form-data POST error with JavaScript, we set the Accept request header to "*/*" to accept all response MIME types.

For instance, we write

const options = {
  //...
  headers: new HttpHeaders({
    Accept: "*/*",
    Authorization:
      "Bearer " + JSON.parse(sessionStorage.getItem("token")).token,
    "Access-Control-Allow-Origin": this.apiURL,
    "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE",
    "Access-Control-Allow-Headers":
      "origin,X-Requested-With,content-type,accept",
    "Access-Control-Allow-Credentials": "true",
  }),
  //...
};
await fetch(url, options);

to create a HttpHeaders object with the Accept header set to "*/*" to set "*/*" as the Accept request header value.

Then we call fetch with the options object to make a request to the url with all the request headers listed.

Conclusion

To fix the fetch Missing boundary in multipart/form-data POST error with JavaScript, we set the Accept request header to "*/*" to accept all response MIME types.

Categories
JavaScript Answers

How to detect click outside div using JavaScript?

Sometimes, we want to detect click outside div using JavaScript.

In this article, we’ll look at how to detect click outside div using JavaScript.

How to detect click outside div using JavaScript?

To detect click outside div using JavaScript, we can check if e.target doesn’t equal the inner element.

For instance, we write

document.getElementById("outer-container").onclick = (e) => {
  if (e.target !== document.getElementById("content-area")) {
    console.log("You clicked outside");
  } else {
    console.log("You clicked inside");
  }
};

to check that e.target doesn’t equal to document.getElementById("content-area").

If it’s not, then we clicked outside of the element with ID content-area.

We add a click listener to the element with ID outer-container by setting onclick to the click listener function.

Conclusion

To detect click outside div using JavaScript, we can check if e.target doesn’t equal the inner element.

Categories
JavaScript Answers

How to update record, and return result with Sequelize and JavaScript?

Sometimes, we want to update record, and return result with Sequelize and JavaScript.

In this article, we’ll look at how to update record, and return result with Sequelize and JavaScript.

How to update record, and return result with Sequelize and JavaScript?

To update record, and return result with Sequelize and JavaScript, we can use the db.connections.update method.

For instance, we write

const result = await db.connections.update(
  {
    user: data.username,
    chatroomID: data.chatroomID,
  },
  {
    where: { socketID: socket.id },
    returning: true,
    plain: true,
  }
);

to call db.connection.update to update the user and chatroomID values.

We set returning to true to return the updated entry as an object with the promise returned by update.

Conclusion

To update record, and return result with Sequelize and JavaScript, we can use the db.connections.update method.

Categories
JavaScript Answers

How to create SVG elements dynamically with JavaScript inside HTML?

Sometimes, we want to create SVG elements dynamically with JavaScript inside HTML.

In this article, we’ll look at how to create SVG elements dynamically with JavaScript inside HTML.

How to create SVG elements dynamically with JavaScript inside HTML?

To create SVG elements dynamically with JavaScript inside HTML, we use the document.createElementNS method.

For instance, we write

const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");

to call the document.createElementNS method to create an svg element with the namespace for svg.

Conclusion

To create SVG elements dynamically with JavaScript inside HTML, we use the document.createElementNS method.

Categories
JavaScript Answers

How to pick element inside iframe using document.getElementById with JavaScript?

Sometimes, we want to pick element inside iframe using document.getElementById with JavaScript.

In this article, we’ll look at how to pick element inside iframe using document.getElementById with JavaScript.

How to pick element inside iframe using document.getElementById with JavaScript?

To pick element inside iframe using document.getElementById with JavaScript, we can use the contentWindow property.

For instance, we write

const el = document
  .getElementById("myframe1")
  .contentWindow.document.getElementById("x");

to select the iframe with getElementById.

Then we use contentWindow.document.getElementById to select the element with ID x in the iframe.

Conclusion

To pick element inside iframe using document.getElementById with JavaScript, we can use the contentWindow property.