Categories
JavaScript Answers

How to sanitize user input before adding it to the DOM in JavaScript?

Sometimes, we want to sanitize user input before adding it to the DOM in JavaScript.

In this article, we’ll look at how to sanitize user input before adding it to the DOM in JavaScript.

How to sanitize user input before adding it to the DOM in JavaScript?

To sanitize user input before adding it to the DOM in JavaScript, we use the string replace method.

For instance, we write

const sanitize = (string) => {
  const map = {
    "&": "&",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&#x27;",
    "/": "&#x2F;",
  };
  const reg = /[&<>"'/]/gi;
  return string.replace(reg, (match) => map[match]);
};

to call string.replace with reg and a callback to replace the characters to be escaped with the corresponding HTML entities.

reg matches all values since it has the g flag.

And the i flag makes replace match in a case-insensitive manner.

Conclusion

To sanitize user input before adding it to the DOM in JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to change property name with JavaScript?

Sometimes, we want to change property name with JavaScript.

In this article, we’ll look at how to change property name with JavaScript.

How to change property name with JavaScript?

To change property name with JavaScript, we use the delete operator.

For instance, we write

a.prop3 = a.prop1;
delete a.prop1;

to assign a.prop1 to a.prop3.

Then we remove a.prop1 from a with delete.

Conclusion

To change property name with JavaScript, we use the delete operator.

Categories
JavaScript Answers

How to get the image before submitting the form with HTML input type=file and JavaScript?

Sometimes, we want to get the image before submitting the form with HTML input type=file and JavaScript.

In this article, we’ll look at how to get the image before submitting the form with HTML input type=file and JavaScript.

How to get the image before submitting the form with HTML input type=file and JavaScript?

To get the image before submitting the form with HTML input type=file and JavaScript, we use the files property.

For instance, we write

<input type="file" onchange="previewFile()" /><br />
<img src="" height="200" alt="Image preview..." />

to add a file input and an img element.

Then we write

const previewFile = () => {
  const preview = document.querySelector("img");
  const file = document.querySelector("input[type=file]").files[0];
  const reader = new FileReader();

  reader.onloadend = () => {
    preview.src = reader.result;
  };

  if (file) {
    reader.readAsDataURL(file);
  } else {
    preview.src = "";
  }
};

to select the img element and file input with querySelector.

We get the select file with document.querySelector("input[type=file]").files[0].

And we read the file into a base64 URL with readAsDataURL.

reader.result has the file as a base64 URL string.

Conclusion

To get the image before submitting the form with HTML input type=file and JavaScript, we use the files property.

Categories
JavaScript Answers

How to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript?

Sometimes, we want to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript.

In this article, we’ll look at how to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript.

How to fix Cannot read property ‘getContext’ of null, using canvas with JavaScript?

To fix Cannot read property ‘getContext’ of null, using canvas with JavaScript, we should make sure the canvas element is loaded before we try to get it.

For instance, we write

<canvas id='canvas'></canvas>

to add a canvas element.

Then we write

const canvas = document.getElementById("canvas");

to select the canvas element with getElementById.

Conclusion

To fix Cannot read property ‘getContext’ of null, using canvas with JavaScript, we should make sure the canvas element is loaded before we try to get it.

Categories
JavaScript Answers

How to push an associative item into an array in JavaScript?

Sometimes, we want to push an associative item into an array in JavaScript.

In this article, we’ll look at how to push an associative item into an array in JavaScript.

How to push an associative item into an array in JavaScript?

To push an associative item into an array in JavaScript, we use an object.

For instance, we write

const obj = {};
const name = "name";
const val = 2;
obj[name] = val;
console.log(obj);

to create the object obj.

Then we add a property with key name and value val into obj with

obj[name] = val;

Conclusion

To push an associative item into an array in JavaScript, we use an object.