1

I have html form for creating new product with image, so after choosing product image my html form submit button will become disabled. I've wrote file input action in javascript.

Here my html form code (piece of code):

<form>
<div class="form-group alert-up-pd">
  <div class="small-12 large-4 columns">
    <label for="">Добавить изображение</label>
    <div class="containers">
      <div class="imageWrapper">
        <img class="image" src=""></div>
      </div>
      <span class="file-upload">
        <input type="file" name="img" class="file-input">
        Выбрать
      </span>
    </div>
  </div>
</div>
</form>

And here is javascript code:

$('.file-input').change(function(e){
  var curElement = $('.image');
  var reader = new FileReader();

  reader.onload = function (e) {
    curElement.attr('src', e.target.result);
  };

  reader.readAsDataURL(this.files[0]);
});

If you need more info pls ask...

1
  • form submit works when I don't choose file Commented Dec 21, 2020 at 16:03

1 Answer 1

2

Your Code seems to work. Try to run that snippet:

$(function(){
    $('.file-input').change(function(e){
    var curElement = $('.image');
    var reader = new FileReader();

    reader.onload = function (e) {
      curElement.attr('src', e.target.result);
    };

    reader.readAsDataURL(this.files[0]);
  });
});
.image
{
  width: 250px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <div class="form-group alert-up-pd">
    <div class="small-12 large-4 columns">
      <label for="">Добавить изображение</label>
      <div class="containers">
        <div class="imageWrapper">
          <img class="image" src="" />
        </div>
        <span class="file-upload">
          <input type="file" name="img" class="file-input" />
          Выбрать
        </span>
      </div>
    </div>
  </div>
</form>

Can you provide an example where the submit button becomes disabled?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for reply. I've found my mistake, it was because of HTML Editor library (for textarea), so I shouldn't have put required attribute to textarea. I'm ashamed infront of you, sorry...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.