Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a file dialog when parent div is clicked using jQuery

Tags:

jquery

file

input

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>
like image 337
user1915190 Avatar asked Dec 03 '25 17:12

user1915190


1 Answers

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/

like image 103
Blender Avatar answered Dec 06 '25 08:12

Blender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!