Q:

Using Jquery functions with Multiple Text Fields With Same ID

I looking for help on how to reference specific text fields with jquery that are generated with javascript.
What I get works only on the first instance of the field reference.
HTML Code: 
Record ID<input type="text" name="ri[]" id="ri1" size="7" style="font-size:0.9em;">

Qty Per Box<input type="text" name="qpb[]" id="qpb1" size="7" style="font-size:0.9em;">

Javascript Used To Generate More Rows Like Above
<!--Dynamically Create New Rows For Data Entry-->
 <script language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                }
            }
        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true === chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }

            }
            }catch(e) {
                alert(e);
            }
        }

    </script>

So what I end up getting is multiple rows with the same id / name. What I simply want to do is validate input on keyup so I decided to use the (this).val method but it only works on the first row. 
<script>
$(document).ready(function() {
    $("#ri1").keyup(function() {
    $(this).val($(this).val().replace(/[^\d]/g, ""));
    });
 });
</script>

Anybody know a better solution to validating input on when generating multiple rows of input fields this way? 
Js fiddle http://jsfiddle.net/nS2LM/22/

A:

Using an ID more than once is not recommended but if you are using the latest jQuery then event delegation is your answer:
<input type="text" name="ri[1]" id="ri1" size="7" style="font-size:0.9em;" class="do_stuff">
<input type="text" name="ri[2]" id="ri2" size="7" style="font-size:0.9em;" class="do_stuff">

<script>
$(document).ready(function() {
    $('body').on('keyup', '.do_stuff', function() {
        $(this).val($(this).val().replace(/[^\d]/g, ""));
    });
});
</script>

The code above binds keyup to the body's child .do_stuff elements so any dynamically created elements with a class of .do_stuff will be caught by the keyup event.

