Categories
JavaScript Answers

How to show the files already stored on server using Dropzone.js and JavaScript?

Sometimes, we want to show the files already stored on server using Dropzone.js and JavaScript.

In this article, we’ll look at how to show the files already stored on server using Dropzone.js and JavaScript.

How to show the files already stored on server using Dropzone.js and JavaScript?

To show the files already stored on server using Dropzone.js and JavaScript, we can set the files list.

For instance, we write

<form id="sample-img" action="/upload" class="dropzone">
  <div class="dz-default dz-message"></div>
</form>

to add a form with a drop zone div.

Then we write

const previewThumbailFromUrl = (opts) => {
  const imgDropzone = Dropzone.forElement("#" + opts.selector);
  const mockFile = {
    name: opts.fileName,
    size: 12345,
    accepted: true,
    kind: "image",
  };
  imgDropzone.emit("addedfile", mockFile);
  imgDropzone.files.push(mockFile);
  imgDropzone.createThumbnailFromUrl(mockFile, opts.imageURL, () => {
    imgDropzone.emit("complete", mockFile);
  });
};

previewThumbailFromUrl({
  selector: "sample-img",
  fileName: "sampleImg",
  imageURL: "/images/sample.png",
});

to define the previewThumbailFromUrl function.

In it, we get the drop zone element with Dropzone.forElement.

Then we use imgDropzone.emit("addedfile", mockFile); to emit the addedFile event with mockFile.

Next we add the mockFile to the list with

imgDropzone.files.push(mockFile);

Then we update the thumbnail with imgDropzone.createThumbnailFromUrl.

Conclusion

To show the files already stored on server using Dropzone.js and JavaScript, we can set the files list.

Categories
JavaScript Answers

How to scroll an iframe with JavaScript?

Sometimes, we want to scroll an iframe with JavaScript.

In this article, we’ll look at how to scroll an iframe with JavaScript.

How to scroll an iframe with JavaScript?

To scroll an iframe with JavaScript, we call the scrollTo method.

For instance, we write

const myIframe = document.getElementById("iframe");
myIframe.onload = () => {
  myIframe.contentWindow.scrollTo(xCoord, yCoord);
};

to select the iframe with getElementById.

Then we call myIframe.contentWindow.scrollTo to scroll the iframe to xCoord, yCoord x-y coordinates in the iframe’s onload callback.

We call scrollTo there to scroll only when the iframe is loaded.

Conclusion

To scroll an iframe with JavaScript, we call the scrollTo method.

Categories
JavaScript Answers

How to remove JSON object key and value with JavaScript?

Sometimes, we want to remove JSON object key and value with JavaScript

In this article, we’ll look at how to remove JSON object key and value with JavaScript.

How to remove JSON object key and value with JavaScript?

To remove JSON object key and value with JavaScript, we use the delete operator.

For instance, we write

const myObject = {
  employeeid: "888888",
  firstName: "tet",
  lastName: "test",
  email: "test@email.com",
  country: "Brasil",
  currentIndustry: "aaaaaaaaaaaaa",
  otherIndustry: "aaaaaaaaaaaaa",
  currentOrganization: "test",
  salary: "1234567",
};

delete myObject["currentIndustry"];

console.log(myObject);

to remove the currentIndustry property from myObject with delete myObject["currentIndustry"];.

Conclusion

To remove JSON object key and value with JavaScript, we use the delete operator.

Categories
JavaScript Answers

How to reload current page without losing any form data with JavaScript?

Sometimes, we want to reload current page without losing any form data with JavaScript.

In this article, we’ll look at how to reload current page without losing any form data with JavaScript.

How to reload current page without losing any form data with JavaScript?

To reload current page without losing any form data with JavaScript, we can save data to local storage and then get the values after the page reloads.

For instance, we write

window.onbeforeunload = () => {
  localStorage.setItem("name", document.querySelector("#inputName").value);
  localStorage.setItem("email", document.querySelector("#inputEmail").value);
  localStorage.setItem("phone", document.querySelector("#inputPhone").value);
  // ...
};

window.onload = () => {
  const name = localStorage.getItem("name");
  if (name !== null) {
  }

  // ...
};

to set window.onbeforeunload to a function that’s called before we unload the page.

We get the values from the inputs and save them to local storage with localStorage.setItem.

Then we set window.onload to a function that’s called when the page is loaded.

In it, we get the local storage entry with the key with getItem.

Conclusion

To reload current page without losing any form data with JavaScript, we can save data to local storage and then get the values after the page reloads.

Categories
JavaScript Answers

How to push object into array with JavaScript?

Sometimes, we want to push object into array with JavaScript.

In this article, we’ll look at how to push object into array with JavaScript.

How to push object into array with JavaScript?

To push object into array with JavaScript, we use the push method.

For instance, we write

const vals = [];
const obj = {};
obj["01"] = n.label;
obj["02"] = n.value;
vals.push(obj);

to call vals.push to append obj as the last entry of the vals array.

We use

const obj = {};
obj["01"] = n.label;
obj["02"] = n.value;

to add the entries.

Conclusion

To push object into array with JavaScript, we use the push method.