4

Sorry if you find this question repetitive, but I looked around, tried, failed, so need your help.

one.html

 <html>
    <head>
    <link rel="stylesheet" href="one.css" >
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="one.js"></script>
    </head>
    <body>
       <div class='some-class' id="add-image">
            <input type="file" id="myfile" style="display:none" />
            <h4> I want to open the file upload dilogue when anywhere of this div has been clicked</h4>
       </div>
   </body>
 </html>

one.css

.some-class{
border: 1px solid grey;
}

h4{
    text-align:center;
}

one.js

$("#add-image").click(function(){
  $("input[id='myfile']").click();
});

It shows sometimes

event.returnValue is deprecated. Please use the standard event.preventDefault() instead.

sometimes

uncaught rangeerror maximum call stack size exceeded

I don't know whats happening, if its a issue with the jquery version? Can you please help me? A working code sample or maybe a jsfiddle? Thank you in advance :)

2
  • Have you tried $('#myfile').click(); or $('#myfile').focus();? Commented Nov 27, 2013 at 13:10
  • tried both. Not working! Commented Nov 27, 2013 at 15:20

3 Answers 3

3

You need to add the click handler within a dom ready handler because when one.js is executed the add-image element is not present in the dom structure

jQuery(function ($) {
    $("#add-image").click(function () {
        $("#myfile").click();
    });
})

Dom Ready

Since the file input element is within the myfile element triggering the click element on the file element will cause a recursive error saying callstack exceeded

The solution is to move the file element out of the myfile element

<input type="file" id="myfile" style="display:none" />
<div class='some-class' id="add-image">
     <h4> I want to open the file upload dilogue when anywhere of this div has been clicked</h4>
</div>

Demo: Fiddle

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

9 Comments

Not working Arun. Throwing error like this: event.returnValue is deprecated. Please use the standard event.preventDefault() instead. Uncaught RangeError: Maximum call stack size exceeded
event.returnValue is deprecated is a warning in new versions of browsers... hopefully jQuery will handle it in the new versions
can you give some more info about the call stack issue
Means? I just put the error warning from the console. There's nothing more.
I know. but the file upload dilogue is not opening.
|
1

change your html to

     <div class='some-class' id="add-image">
        <h4> I want to open the file upload dilogue when anywhere of this div has been clicked</h4>
    </div>
    <input type="file" id="myfile" style="display:none" />

Comments

1

I thin kyou have missed the dom ready handler.
try this:

$(document).ready(function(){
  $("#add-image").click(function(){
     $("#myfile").trigger('click');
  });
});

1 Comment

event.returnValue is deprecated. Please use the standard event.preventDefault() instead. Uncaught RangeError: Maximum call stack size exceeded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.