0

i have javascript function. i am using that function to translate english language to another language in a text field. as below form when a person type in box01, box02 shows typed english content in translated mode.

 <form id="txtBox" action="ps.php" enctype="multipart/form-data" method="post">

 <textarea  id="box01" onkeyup="startText_sin();" onselect="startText_sin();" onclick="startText_sin();" rows="2" placeholder ="Type in Here"></textarea>

 <textarea  id="box02" rows="2" placeholder ="Translated"></textarea>

 </form>

but i have few of these forms in my single page and the text areas have diffrent names, like bellow,

 <textarea  id="txt_sin01"></textarea>
 <textarea  id="txt_trn02"></textarea>

how can i can set function to be work with all of these text areas ?
my function is **startText_sin()**

in function i choose text area like this text = document.getElementById("box01");

any help.

______function________

` //array goes here

function startText_sin() {

var s,r,v;
//text = document.txtBox.box1.value;
text_singlish = document.getElementById("box1_singlish").value;
//special consonents
for (var i=0; i<specialConsonants.length; i++){
    text_singlish = text_singlish.replace(specialConsonants[i], specialConsonantsUni[i]);
}
//consonents + special Chars
for (var i=0; i<specialCharUni.length; i++){
    for (var j=0;j<consonants.length;j++){
        s = consonants[j] + specialChar[i];
        v = consonantsUni[j] + specialCharUni[i];
        r = new RegExp(s, "g");
        text_singlish = text_singlish.replace(r, v);
    }
}
//consonants + Rakaransha + vowel modifiers
for (var j=0;j<consonants.length;j++){
    for (var i=0;i<vowels.length;i++){
        s = consonants[j] + "r" + vowels[i];
        v = consonantsUni[j] + "්‍ර" + vowelModifiersUni[i];
        r = new RegExp(s, "g");
        text_singlish = text_singlish.replace(r, v);
    }
    s = consonants[j] + "r";
    v = consonantsUni[j] + "්‍ර";
    r = new RegExp(s, "g");
    text_singlish = text_singlish.replace(r, v);
}
//consonents + vowel modifiers
for (var i=0;i<consonants.length;i++){
    for (var j=0;j<nVowels;j++){ 
        s = consonants[i]+vowels[j];
        v = consonantsUni[i] + vowelModifiersUni[j];
        r = new RegExp(s, "g");
        text_singlish = text_singlish.replace(r, v);
    }
}

//consonents + HAL
for (var i=0; i<consonants.length; i++){
    r = new RegExp(consonants[i], "g");
    text_singlish = text_singlish.replace(r, consonantsUni[i]+"්");
}

//vowels
for (var i=0; i<vowels.length; i++){
    r = new RegExp(vowels[i], "g");
    text_singlish = text_singlish.replace(r, vowelsUni[i]);
}

document.getElementById("box2_singlish").value=text_singlish; //wirte on page in sihala

}

`

2
  • @oGeez check the code, i updated Commented Nov 2, 2013 at 14:37
  • To be honest, we don't really need to see the function :) Commented Nov 2, 2013 at 14:41

1 Answer 1

1

Pass the id of the textarea into the function

var startText_sin = function (textId) {
    text = document.getElementById(textId);
    // do stuff
};

And the call:

startText_sin("box01");

And an example doing it inline:

<textarea  id="box01" onkeyup="startText_sin("box01");" onselect="startText_sin("box01");" onclick="startText_sin("box01");" rows="2" placeholder ="Type in Here"></textarea>

I must say though, this is a horrible way to write a piece of software. Rather than using onselect and onclick for each individual element you can watch for events on the page and process them. I understand that this isn't where you are at but in the future, and for anyone reading this. If you are writing anything more complex than this then you shouldn't be using these methods.

Sign up to request clarification or add additional context in comments.

1 Comment

please can you tell me how to do that, in breef ?

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.