javascript - If element exists in a document -
it's possible situation function use out of date jquery element. example when use closer , callback:
$("input").each(function () { var input = $(this); //here example remove input dom //and insert new 1 input $.ajax(url, function(){ fun(input) } }); function fun(input){ //do input.val('newvalue') }
questions are: how can sure reference on variable still right. , if element has been reacreated how can new reference on new input (input doesnt have id , classes)?
update1: made small update. use old input reference in function fun. , newvalue not apply new input coz current input old value.
you can check whether element exists checking length of jquery object. example:
if($(this).length < 1) { // element no longer exists in dom }
however, can use jquery's on()
function bind events elements exist now, or in future, , perhaps that's better approach 1 you're working with. example:
$('body').on('click', 'input', function(e) { // place click handler here. });
this article might prove worthwhile read you.