-1

I have this error, and I don't know what to do

This is my code

js:

var uploadField = document.getElementById("file");

        console.log(uploadField);
        
            uploadField.onchange = function() {
                console.log("new")
                if(this.files[0].size > 2200000){
                        alert("File is too big!");
                        this.value = "";
            };
        };

html:

<input id ="post-input" id type="file" placeholder="Foto do Cartaz" name="image" accept=".png,.jpeg,.jpg,.gif" class="file-input" style="color: black;"/>

What's the best solution, I've searched and none of the solutions that help everybody, helped me.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75

2 Answers2

0

Seems a typo(have a duplicate id with no value),needs to change

  id ="post-input" id type="file"

to

   id ="post-input" type="file"

and change the way you get it

   var uploadField = document.getElementById("post-input");
flyingfox
  • 13,414
  • 3
  • 24
  • 39
0

Simply a couple of typos.

Change the string passed into .getElementById to the same as the input id atribute:

var uploadField = document.getElementById('post-input');
console.log(uploadField);
uploadField.onchange = function () {
  console.log('new');
  if (this.files[0].size > 2200000) {
    alert('File is too big!');
    this.value = '';
  }
};

Then remove the duplicated id atribute with no value asigned:

<input
  id="post-input"
  type="file"
  placeholder="Foto do Cartaz"
  name="image"
  accept=".png,.jpeg,.jpg,.gif"
  class="file-input"
  style="color: black;"
/>

Hope this helps.

damonholden
  • 1,062
  • 4
  • 17