0

I would like to change my code that it work for form as button will enable validation currently so for any button in form i will create a input type = "button".

I'm currently studying this code where it allow me to change picture and load picture however i am not allow to use input to button as the picture will not load and result error. Here the example of how it look like.

I would like to use input type for button still as I cant use button since this is inside a form where if i click on a button tag it will appear with validation.

I have try changing the code to:

<input for="file" type="button" style="margin-left: 45px; height: 30px;" value="Select Image" style="cursor: pointer;">

But after that it don't allow me to select pic or load:

enter image description here

This is what the code look like:

var loadFile = function(event) {
    var image = document.getElementById('output');
    image.src = URL.createObjectURL(event.target.files[0]);
};
<p>
    <input type="file" accept="image/*" name="image" id="file" onchange="loadFile(event)" style="display: none;">
</p>

<p>
    <img src="https://mdbootstrap.com/img/Photos/Others/placeholder-avatar.jpg" id="output" width="200" height="200" style="border-radius: 50%;" />
</p><br/>

<label for="file" style="cursor: pointer; padding-left: 3px; padding-top: 1px; position: static; margin-left: 45px; height: 30px;">Select Image</label>

Is there a way for me to use input for button?

Sercan
  • 4,739
  • 3
  • 17
  • 36
Flow
  • 271
  • 2
  • 11

1 Answers1

0

You could "redirect" the click from the image to the input like this:

$("#output").click((e)=>{
   $('#file').click();
})

(I didn't test it)

MoPaMo
  • 517
  • 6
  • 24