user1915190
user1915190

Reputation: 307

Open a file dialog when parent div is clicked using jQuery

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>

Upvotes: 2

Views: 10511

Answers (2)

Swarne27
Swarne27

Reputation: 5747

Try using trigger(),

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

Upvotes: 0

Blender
Blender

Reputation: 298532

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/

Upvotes: 10

Related Questions