2

I want to open a file dialog when the parent div is clicked. If I click the first parent div, it should only open the first input file.

<div class="fileupload">
    <input type="file" class="file" name="image" />
</div>

<div class="fileupload">
    <input type="file" class="file" name="attachement" />
</div>
Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
user1915190
  • 307
  • 1
  • 2
  • 14
  • see this once http://stackoverflow.com/questions/6460185/cross-browser-open-file-browse-dialog – Anshu Jan 02 '13 at 10:21

2 Answers2

10

Just trigger the click event on the input element:

$('.fileupload').click(function(e) {
    $(this).find('input[type="file"]').click();
});

$('.fileupload input').click(function(e) {
    e.stopPropagation();
});​

Demo: http://jsfiddle.net/EctCK/

Blender
  • 289,723
  • 53
  • 439
  • 496
0

Try using trigger(),

$(document).ready(function() {
    $(this).parents(".fileupload").find("input[type='file']").trigger('click');
});
Swarne27
  • 5,521
  • 7
  • 26
  • 41