I want to capture input charcters in text box, convert then according to a table and put them back in text box as user types.
Should do the trick. Now works wherever caret is and even when you copy/paste WECZ into field (if that matters)
var conversionMap = {W:1,E:2,R:3,S:4,D:5,F:6,Z:7,X:8,C:9};
function alphaToNum(){
var field = document.getElementById('idMyText');
var value = field.value.split('');
var i = 0, len = value.length;
for(i;i<len;i++){
if (conversionMap[value[i]]) {
value[i] = conversionMap[value[i]];
}
}
field.value = value.join('');
// prevent memory leak.
field = null;
}
its working good until we didn't use utf-8 chars like öéáí etc... any idea for repair this "leak" ?
Should do the trick. Now works wherever caret is and even when you copy/paste WECZ into field (if that matters)
var conversionMap = {W:1,E:2,R:3,S:4,D:5,F:6,Z:7,X:8,C:9};
function alphaToNum(){
var field = document.getElementById('idMyText');
var value = field.value.split('');
var i = 0, len = value.length;
for(i;i<len;i++){
if (conversionMap[value[i]]) {
value[i] = conversionMap[value[i]];
}
}
field.value = value.join('');
// prevent memory leak.
field = null;
}
** Edit after Tim Downs comment **
It might be better to do this when the value changes in the input, rather than when the key is pressed. Otherwise, other forms of input (pasting value) will bypass this substitution.