Javascript - Use variable inside push string

Viewed 51

I am trying to have the data-max value display dynamically inside of the err.push() string to show how many files that e allows.

How do I get the data-max variable to dynamically appear in the error message string?

  let maxFileNum = e.target.getAttribute('data-max'); //Maximum number of files
  if (fileList.files.length > maxFileNum) {
    let tmpf = [];
    err.push('Limit of ${maxFileNum} images allowed');
    fileList.files = tmpf;
  }
1 Answers

You will have to use back ticks (``) to represent a string literal:

  let maxFileNum = e.target.getAttribute('data-max'); //Maximum number of files
  if (fileList.files.length > maxFileNum) {
    let tmpf = [];
    err.push(`Limit of ${maxFileNum} images allowed`);
    fileList.files = tmpf;
  }
Related