...

View Full Version : Key pad character map changes on elements within a table row added through DOM



aderes
04-21-2009, 04:52 PM
Hi Everyone:

I Ďm having a little problem. I currently have a table containing form elements. By default the table has one row. Certain fields need to be numbers so Iím using javascript to validate them. Everything works fine on the original row. Iím using DOM to add rows to the table. But when I try to validate the field to check if itís a number, the key pad numbers donít register as numbers any more. It still works on row 1 (the original row) but not the added rows.

Example: when I press the number 1 keypad key and alert the results, on elements of the first row I get a key number of 49, but on elements that were added through DOM I get 97 which is actually ĎAí not the number 1.


Hereís an example of the code Iím using to add rows to the table



var tRow = document.createElement("tr");

cell = document.createElement("td");
cell.id = ďrow_id";
input = document.createElement("input");
input.setAttribute("type","textbox");
input.id="txt";
input.className="";

if(navigator.appName == "Microsoft Internet Explorer")
{
input.onkeydown = function(){return onlyNum(event)};
}
else
{

input.setAttribute("onkeydown","return onlyNum(event)");
}
cell.appendChild(input);
tRow.appendChild(cell);

Hereís my number checking function


function onlyNum(e)
{

var keynum;
var numcheck = /\d/;



if(window.event)
{
keynum = e.keyCode;
}
else
{
keynum = e.which;
}

if((keynum == 0)||(keynum == 8)||(keynum == 46)|| numcheck.test(String.fromCharCode(keynum)))
{
return true;
}
else
{
return false;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum