Rbex Rbex -4 years ago 111
jQuery Question

Dynamic input type text doesn't work Jquery

I have a dynamic input type with a class of integer but it doesn't and it doesn't work. If I don't populate it dynamically it works.

Here is the code

$(function(){
$.integer = function(e){
var arr = "1234567890";
var code;
if (window.event)
code = e.keyCode;
else
code = e.which;
var char = keychar = String.fromCharCode(code);
if (arr.indexOf(char) == -1){
return false;
}
};

$(".integer").on('keypress blur', function(e){
return $.integer(e);
});



});


It works if it's not dynamic, all I had to do is add a class integer.

<input type="text" class="integer">


I also used this code
.on()
method in jquery and it actually alerts but I can enter other characters besides "1234567890".

$('#table').on('keyup blur change', '.integer', function(e){
alert('asdf');
return $.integer(e);
})

Answer Source

it's working fine.check here:-

$('#table').append('<input type="text" class="integer">');// dynamically added input-box

$(".integer").on('keypress blur', function(e){
  return $.integer(e);
});

$(function(){
    $.integer = function(e){
        var arr = "1234567890";
        var code;
        if (window.event)
            code = e.keyCode;
        else
            code = e.which;
        var char = keychar = String.fromCharCode(code);
        if (arr.indexOf(char) == -1){
            return false;
        }
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id = "table"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download