简体   繁体   中英

Copy only the entered character from one text box to another

I have two text box #textBox1 and #textBox2 .

User will input to #textBox1 and all the values need to be copied to #textBox2 automatically [I have a onkeyup event here]. Only copy and paste from one to another is not a problem. My requirement is once user press space or enter my English word from #textBox1 will convert to local language but I need only the English version on #textBox2 .

See the example below:

textBox1 = "Bhuwan", textBox2="Bhuwan"

once user press space bar

textBox1 = "भुवन ", textBox2="Bhuwan "

textBox1 = "भुवन Gautam", textBox2="Bhuwan Gautam"

Again once user press space bar

textBox1 = "भुवन गौतम", textBox2="Bhuwan Gautam"

I have used the following but it is not complete if the user use backspace on #textBox1 .

$("#textBox1").keyup(function(e) {
            $('#textBox2').val($('#textBox2').val()+String.fromCharCode(e.which));
});

I know there is a better solution. How do I achieve this either from jquery or javascript ?

I think You can go through this solution Click

I will highlight some points.

Use Google translation API. Easy to use. For example the following translates Spanish to English. To translate from and to other languages, simply change 'es' and 'en'

google.load("language", "1");

function initialize() {
    var content = document.getElementById('content');
    content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="translation"/>';
    var text = document.getElementById("text").innerHTML;
    google.language.translate(text, 'es', 'en', function(result) {
        var translated = document.getElementById("translation");
        if (result.translation) {
            translated.innerHTML = result.translation;
        }
    });
}
google.setOnLoadCallback(initialize);

try google translate : http://code.google.com/apis/language/translate/overview.html

You can also use this JQuery plugin http://www.openxrest.com/translatejs

You can use bing translate .. http://setahost.com/bing-translate-api-with-jquery-ajax/ Bing api is still free

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM