Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery keyup not working

For some reason, when I add a class to various inputs (after a user presses submit), this function does not record keypresses being made within those inputs. I must be doing something fundamentally wrong as I've tried $('incomplete_required').keyup(){} and what is below, as it seems .on() is more modern..

$(document).ready(function(){
    $('.incomplete_required').on("keyup", function(){
        console.log('keyed');
    });
});

What have I done wrong here?

Thank you

like image 712
1252748 Avatar asked Sep 05 '12 21:09

1252748


1 Answers

If you are dynamically changing elements in the dom - it is best to delegate the event to an existing parent in the dom

$(document).ready(function(){
    $('body').on("keyup",'.incomplete_required', function(){
        console.log('keyed');
    });
});

Obviously replacing body with the closest parent element that exists on dom ready

By delegating - the event will bubble up to the "parent" element - which is where the event is handled

like image 185
wirey00 Avatar answered Oct 17 '22 15:10

wirey00