Categories
JavaScript Answers

How to make a simple image upload using JavaScript and HTML?

Sometimes, we want to make a simple image upload using JavaScript and HTML.

In this article, we’ll look at how to make a simple image upload using JavaScript and HTML.

How to make a simple image upload using JavaScript and HTML?

To make a simple image upload using JavaScript and HTML, we can add a file input and img element.

And then we can get the selected file from the file input and show it in the img element.

For instance, we write

<input type="file" /> <br /><img id="myImg" src="#" />

to add the file input and img element.

Then we write

document.querySelector('input[type="file"]').addEventListener("change", (e) => {
  if (e.target?.files?.[0]) {
    const img = document.querySelector("img");
    img.onload = () => {
      URL.revokeObjectURL(img.src);
    };

    img.src = URL.createObjectURL(e.target?.files[0]);
  }
});

to select the file input with querySelector.

And then then we listen for its change event with addEventListener.

In the event listener, we get the file from e.target?.files?.[0].

Then we get the img element with querySelector.

We set the src property of the img element to the URL base64 string that we get by calling createObjectURL with the file object.

Then when the image is loaded, we call revokeObjectURL to clear the resources for creating the URL.

Conclusion

To make a simple image upload using JavaScript and HTML, we can add a file input and img element.

And then we can get the selected file from the file input and show it in the img element.

Categories
JavaScript Answers

How to open a new tab in the background with JavaScript?

Sometimes, we want to open a new tab in the background with JavaScript.

In this article, we’ll look at how to open a new tab in the background with JavaScript.

How to open a new tab in the background with JavaScript?

To open a new tab in the background with JavaScript, we can simulate pressing ctrl-click on a link programmatically.

For instance, we write

const a = document.createElement("a");
a.href = "http://www.example.com/";
const evt = document.createEvent("MouseEvents");
evt.initMouseEvent(
  "click",
  true,
  true,
  window,
  0,
  0,
  0,
  0,
  0,
  true,
  false,
  false,
  false,
  0,
  null
);
a.dispatchEvent(evt);

to call createElement to create a link.

We set its href property to the URL we want to go to.

Then we call createEvent to create a mouse event.

And we call initMouseEvent to create a mouse event with the 10th argument set to true to enable pressing ctrl.

Next, we call dispatchEvent with evt to dispatch the mouse event to open the link in the background.

Conclusion

To open a new tab in the background with JavaScript, we can simulate pressing ctrl-click on a link programmatically.

Categories
JavaScript Answers

How to render an array.map() in React?

Sometimes, we want to render an array.map() in React.

In this article, we’ll look at how to render an array.map() in React.

How to render an array.map() in React?

To render an array.map() in React, we call map with a function that returns the elements we want.

For instance, we write

data.map((item, key) => {
  return (
    <tr key={key}>
      <td>{item.heading}</td>
      <td>{item.date}</td>
      <td>{item.status}</td>
    </tr>
  );
});

to call data.map with a function that returns tr elements.

We set the key prop to a unique value so that React can identify the component being rendered.

item is the entry in data being looped through.

Conclusion

To render an array.map() in React, we call map with a function that returns the elements we want.

Categories
JavaScript Answers

How to use JavaScript fetch to make DELETE and PUT requests?

Sometimes, we want to use JavaScript fetch to make DELETE and PUT requests.

In this article, we’ll look at how to use JavaScript fetch to make DELETE and PUT requests.

How to use JavaScript fetch to make DELETE and PUT requests?

To use JavaScript fetch to make DELETE and PUT requests, we can call fetch with the method option.

For instance, we write

const createNewProfile = async (profile) => {
  const formData = new FormData();
  formData.append("first_name", profile.firstName);
  formData.append("last_name", profile.lastName);
  formData.append("email", profile.email);

  const response = await fetch("http://example.com/api/v1/registration", {
    method: "POST",
    body: formData,
  });
  return response.json();
};

const json = await createNewProfile(profile);

to call fetch with an object that has the method property set to a string with the request method.

We set body to the request body.

And then we return the response JSON promise by returning response.json.

Conclusion

To use JavaScript fetch to make DELETE and PUT requests, we can call fetch with the method option.

Categories
JavaScript Answers

How to go from one page to another page using JavaScript?

Sometimes, we want to go from one page to another page using JavaScript.

In this article, we’ll look at how to go from one page to another page using JavaScript.

How to go from one page to another page using JavaScript?

To go from one page to another page using JavaScript, we set the window.location.href property to the string with the URL we want to navigate to.

For instance, we write

window.location.href = "sample.html";

to set window.location.href to "sample.html" to navigate to sample.html.

Conclusion

To go from one page to another page using JavaScript, we set the window.location.href property to the string with the URL we want to navigate to.