Reputation: 781
I cant get this to work. I am trying to replace certain characters when a key is pressed. It works fine when I use the variable replace_list instead of replace_list["russian"], but I need different "replace lists" for other things. What am I doing wrong?
<script type="text/javascript" src="javascript/jquery.js"></script>
<input id=answer>
<script>
replace_list = ["russian": {'a' : 'b', 'c' : 'd'}];
$(document).ready(function () {
$("#answer").keydown(function () {
var text = $(this).val();
$.each(replace_list["russian"], function (index, value) {
if (index == text.substring(text.length - value.length)) {
$("#answer").val(text.substring(0, text.length - value.length) + value);
}
});
});
})
</script>
Upvotes: 1
Views: 395
Reputation: 173552
First, you should properly define replace_list
; I will assume you're going to have multiple replace lists that you would like to apply in certain situations:
var replace_list = {
'russian': {a: 'b', b: 'c', c: 'd'}
};
Then:
var text = $(this).val();
for (var search in replace_list['russian']) {
text = text.replace(search, replace_list['russian'][search]);
}
$('#answer').val(text);
In action: http://jsfiddle.net/k8Dhg/
Upvotes: 1
Reputation: 21881
You have to create the array first (or adjust the assignment)
var replace_list = [
{'a' : 'а', 'A' : 'А', 'b' : 'б'}, // replace_list[0]
{'B' : 'Б', 'v' : 'в', 'V' : 'В'} // replace_list[1]
]
Edit
To meet your requirements (replace_list["russian"]
) from the comment
var replace_list = {
"russian": {'a' : 'а', 'A' : 'А', 'b' : 'б'},
"english": {'a' : 'а', 'A' : 'A', 'b' : 'B'}
}
Upvotes: 1
Reputation: 1362
var replace_list = new Object();
replace_list["russian"] = {'a' : 'а', 'A' : 'А', 'b' : 'б', 'B' : 'Б', 'v' : 'в', 'V' : 'В', 'g' : 'г', 'G' : 'Г', 'd' : 'д', 'D' : 'Д', 'ye' : 'е', 'YE' : 'Е', 'yo' : 'ё', 'YO' : 'Ё', 'zh' : 'ж', 'ZH' : 'Ж', 'z' : 'з', 'Z' : 'З', 'i' : 'и', 'I' : 'И', 'j' : 'й', 'J' : 'Й', 'k' : 'к', 'K' : 'К', 'l' : 'л', 'L' : 'Л', 'm' : 'м', 'M' : 'М', 'n' : 'н', 'N' : 'Н', 'o' : 'о', 'O' : 'О', 'p' : 'п', 'P' : 'П', 'r' : 'р', 'R' : 'Р', 's' : 'с', 'S' : 'С', 't' : 'т', 'T' : 'Т', 'u' : 'у', 'U' : 'У', 'f' : 'ф', 'F' : 'Ф', 'kh' : 'х', 'KH' : 'Х', 'ts' : 'ц', 'TS' : 'Ц', 'ch' : 'ч', 'CH' : 'Ч', 'sh' : 'ш', 'SH' : 'Ш', 'shch' : 'щ', 'SHCH' : 'Щ', '\"' : 'ъ', '\"' : 'Ъ', 'y' : 'ы', 'Y' : 'Ы', '\'' : 'ь', '\'' : 'Ь', 'e' : 'э', 'E' : 'Э', 'yu' : 'ю', 'YU' : 'Ю', 'ya' : 'я', 'YA' : 'Я'};
now you can get value of 'a' in russian as
replace_list["russian"]['a']
In this way you can make your list language specific and extend it to as much languages as you wish
Upvotes: 0