AJAX API for Google transliteration: is it possible to make all input fields on a page transliterated?

I used the Google AJAX Transliteration API and everything will be fine with me.

http://code.google.com/apis/ajaxlanguage/documentation/referenceTransliteration.html

I currently have a project in which I need all the input fields on each page (input tags and textarea) for transliteration, while these input fields differ from page to page (dynamic). As I know, I have to call the makeTransliteratable method (elementIds, opt_options) in an API call to define input fields for transliteration, in which case I cannot predefine these fields manually. Is there any way to achieve this?

Thanks in advance

+3
source share
1 answer

To paraphrase what you ask for: you would like to collect all the input data on the page that meet certain criteria, and then pass them to the api.

A quick look at the API link says makeTransliteratable will accept an array of id strings or an array of elements. Since we do not know the identifiers of the elements in front of the hand, we will pass an array of elements.

So how to get an array of elements?

I will show you two ways: the hard way and the easy way.

First, to get all the text areas, we can do this using the document.getElementsByTagName API:

var textareas = document.getElementsByTagName("textarea");

, , .. type, , :

function selectElementsWithTypeAttribute(elements, type)
{
    var results = [];
    for (var i = 0; i < elements.length; i++)
    {
        if (elements[i].getAttribute("type") == type)
        {
            results.push(elements[i]);
        }
    }
    return results;
}

, :

var inputs = document.getElementsByTagName("input")
var textInputs = selectElementsWithTypeAttribute(textInputs, "text");

, , api:

var allTextBoxes = [].concat(textareas).concat(textInputs);
makeTransliteratable(allTextBoxes, /* options here */);

, , . jQuery (google it), :

var allTextBoxes = $("input[type='text'], textarea").toArray();
makeTransliteratable(allTextBoxes, /* options here */);

CSS "" . toArray, , makeTransliteratable.

, ,

+4

Source: https://habr.com/ru/post/1741650/


All Articles