0

So I got this button:

<button class="upload-button" type="button">Upload File</button> 

which I'm using to cover this default input/browse button:

<input type="file" id="fileinput" name="uploads[]" multiple="multiple"> 

however when I'm using this script:

$('.upload-button').on('click', function (){
    $('#upload-input').click();
});

It does not seem to work, what i want to do is trigger the browse file after the fake button is clicked, i found another post on here where they said that this is because of the visibility of the file input is set to hidden, but when i tried to set it to default, it still did not work. What can the problem be here?

1
  • 1
    You sure you are pointing to the right input? like $('#fileinput'').click(); instead of $('#upload-input').click(); Commented Dec 8, 2016 at 10:10

2 Answers 2

2

Use trigger :

 $('#upload-input').trigger( "click" );
Sign up to request clarification or add additional context in comments.

1 Comment

.click() is the same as .trigger('click')
0

Try to hit the DOM object instead :

$('#upload-input')[0].click();

NOTE : make sure that you've the id upload-input (looks like you need #fileinput instead).

Hope this helps.

$('.upload-button').on('click', function (){
  $('#fileinput')[0].click();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="upload-button" type="button">Upload File</button> 
<input type="file" id="fileinput" name="uploads[]" multiple="multiple">

Comments

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.