Search code examples
javascriptjqueryhtmljquery-file-upload

How to update value of an attribute of an element, then trigger click on it?


I would like to use jQuery to update value of attribute "accept" of an input element with type = "file", then active the "click" event of it. But after the value of "accept" is changed, nothing happens. This is my code

//html elements
<button type="button" class="abc" title="Select files from your computer" id="upload" onclick="updateExtension()"></button>
<input type="file" name="image" id="image" multiple="multiple" /> //this input field is hidden and is applied with jquery-file-upload

//jQuery function
function updateExtension(){
  $('#image').attr('accept', '.jpg, .png');
  $('#image').click();
}

Solution

  • You can simply use :

    $( "#image" ).trigger( "click" );
    

    For more information on jQuery trigger click here.