I have several inputs that I would like to ensure that are whole numbers only. Newer browsers understand type="number", but we don't live in a perfect world. Because these fields are thrown across several places in the system I would like to devise a JQuery (using version 1.7.1) solution that I can list them in an array and have them loop through on DOM ready and get bound. Note that some of these inputs come in from AJAX requests so the standard "on" method does not work.
$(document).live("keyup", "#myInput", function(){})
Works fine, however, does not work when within a loop.
The above fiddle shows the elements working as expected, but change the ".on" to ".live", or ".bind" and the event no longer gets bound properly.
So to keep my code clean and not having to manually bind dozens of these elements how can I get ".live" to work within the loop?
var arr = [
$("#myInput"),
$("#differentInput"),
$("#strangeText"),
$("#moreUniqueValues")
]
function callBack($el){
return function(){
$("div.result").text("you typed in "+ $el.selector);
}
};
$(function(){
for(var i = 0; i < arr.length; i++){
$(document).on("keyup", arr[i].selector, callBack(arr[i]));
$("div.loopText").append("<div>"+ arr[i].selector + " - has been bound </div>")
};
});