1

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>
2
  • If you're writing replace_list[0] = ... without having first defined replace_list that will cause an error. You can fix this by first creating an array - var replace_list = [] - or by making your assignment an array assignment: var replace_list = [{ ... }]. Other than that, your code does not seem to produce any meaningful difference in outcome depending on whether your object is placed at replace_list or replace_list[0]. Commented May 21, 2012 at 6:38
  • Your current code looks correct except for the line ["russian": {'a' : 'b', 'c' : 'd'}] which should read {"russian": {'a' : 'b', 'c' : 'd'}} Commented May 21, 2012 at 8:21

3 Answers 3

1

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'}
}
Sign up to request clarification or add additional context in comments.

4 Comments

Something like that is exactly what i need! But how would I go about making the first entry replace_list["russian"] ?
make replace_list as an Object...and then try defining each as replace_list["russian"] = {<your definitions>},replace_list["german"]={<another list>}, and so on
It still doesn't work, I edited my question, try and copy paste the code
@Stuffle: You've used the wrong brackets. You need the curly brackets { } instead of the square ones [ ]
1

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/

6 Comments

yes it is. It transliterates English letters into Russian cyrillic symbols in this example
I can't get it to work. I really suck at Javascript so I am sure thats why. Anyway, the function needs to be able to handle replacements that are longer than 1 char. Also I would love if there was a solution that didn't mean I would have to change too much. It just seems strange to me that everything works with replace_list, but not with replace_list[0]...
Thanks for the help Jack, but what if I needed it to be replace_list["russian"] instead of just replace_list[0]? I know I should have written that in the question, sorry.
Doesn't matter. Just var replace_list = {"russian": { your replacement list here }}
I cant get it to work. I edited my question, try and copy paste the code. I couldnt get it to work with your modifications either :(
|
0
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

2 Comments

But it doesn't work with .each for some reason, which is exactly my problem :(
for a map/object, .each() function works as $.each(map,function(key,value){...});//where map[key] = value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.