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> 
1

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply, When i try this, i'm getting this error 'Uncaught SyntaxError: Unexpected token } '
@user1915190: That's a syntax error somewhere in your script.
When I click the div, my browser is crashed
My scenario was a little different but this answer really helped me to shape my code.
0

Try using trigger(),

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

2 Comments

Thank you, When I click the parent div, my browser got crashed
try again and try with other browsers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.