Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .click() gives me "stack size exceeded"

What in my code gives me this error?

jquery-2.1.3.min.js:3 Uncaught RangeError: Maximum call stack size exceeded

HTML

<form id="addPhotoForm">
    <img class="pull-right" src="" width="100px" height="140px"/>
    <input type="file" id="addPhotoInput" name="addPhotoInput" style="display: none;"/> 
</form>

JS

//On click Photo
$('#addPhotoForm').on('click', function(){
    //Check if usrname exist
    var usrname = $('#usrname').val();
    if(usrname){
        $('#addPhotoInput').trigger('click');
    }
    else{
        alert("!");
    }
})

How can i solve this error?
I'm trying to open the file dialog when clicking on the form.

UPDATE
I tried:

$('#addPhotoInput').click();
like image 203
Björn C Avatar asked Jun 29 '16 09:06

Björn C


1 Answers

To avoid any click event handler bound using jQuery to get fired, and still keep event propagation (useful if any delegate click event is bound too), you could call native DOM click() method instead:

$('#addPhotoInput')[0].click();

Or:

$('#addPhotoInput').get(0).click();

Which are the same as:

document.getElementById('addPhotoInput').click();
like image 147
A. Wolff Avatar answered Oct 23 '22 23:10

A. Wolff