I have styled file input:
<div class="fakeFileContainer">
<div class="fakeFile">Dołącz brief</div>
<input id="file" type="file" name="file"/>
</div>
for this part of code I have some line of js:
var fileInput = $('#contact #file')
fileInput.change(function(){
$this = $(this);
$('#contact form .fakeFile').text($this.val());
})
$('#contact form .fakeFileContainer').on('click', function () {
fileInput.click(); //looping here
}).show();
After click on .fakeFileContainer
I've got this error msg in console:
Uncaught RangeError: Maximum call stack size exceeded
It's caused by loop, but I don't know why this loop formed here. Can comeone explain me reason of this situation?
Your clicks on fileInput
are handled by that function too (because the even bubbles up to the container). So the code says: "when clicked, trigger another click event". That second click event causes the same question to be asked again, and the answer is, again, to fire another click event. Thus, an infinite loop.
The triggered click event bubbles up, and you catch it right on the parent element to trigger the next click - creating an infinite loop. To prevent this, you can either
stopPropagation
of click events on the inputsrcElement
).fakeFile
div instead of the surrounding container.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With