I’ve struggled a bit to find out how to access a textarea (and select box) that was presented in a Flex AIR HTML component.
Wasn’t much work in the end, but here’s an example (by using babelfish’s translation page) of how to access multiple elements from Flex:
<?xml version="1.0" encoding="utf-8"?><mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="1024" height="768"><mx:Script><![CDATA[/*** This example describes how to access HTML elements from Flex AIR AS3.* It makes use of the babelfish translation website to access the translation elements* and then submit the specified translation to babelfish.** @Author: S.Radovanovic* @Url: http://www.saskovic.com/blog/?p=7* @Date: 05-03-2008*/import mx.controls.Alert;//Our access to the HTML Domprivate var domWindow:Object;private var blnAlertThrown:Boolean = false;/*** Find the corresponding element and be able to access it from Flex**/private function initDomWindow(event:Event):void {//Retrieve a link to the textareadomWindow = event.currentTarget.domWindow;//Set a trigger (if not done yet)if( !blnAlertThrown ) {Alert.show("Click ok to set access HTML elements from Flex", "Access HTML elements", Alert.OK, this, setTranslationValues);blnAlertThrown = true; //Make sure we don't loop}}/*** Retrieve a reference to a specific HTML element specified by name*/private function getElementFromHTML(elementName:String):Object {var arrayContainingAllFoundElements:Object = domWindow.document.getElementsByName(elementName);//We could do some checks here to see if anything was found, but I leave that up to the you...
//Always return the first element (there also should only be one)return arrayContainingAllFoundElements[0];}/*** Set the elements in the webpage to your likings.*/private function setTranslationValues(event:Event):void {//Get access to the translation text area and pre-fill itvar translationTextArea:Object = getElementFromHTML("trtext");translationTextArea.value = "Set my own values from Flex!";//Loop through the possible language options of the selectbox and set the one we want (in this case english => dutch)var languageSelectbox:Object = getElementFromHTML("lp");for( var i:int = 0; i < languageSelectbox.options.length; i++ ) {trace(i + ' - ' + languageSelectbox.options[i].value);if( languageSelectbox.options[i].value == "en_nl" ) {languageSelectbox.selectedIndex = i;}}Alert.show("Submit the values for translation by babelfish", "Translate", Alert.OK, this, submitTranslation);}/*** Hit submit!*/public function submitTranslation(event:Event):void {//And finally hit translate!var submitButton:Object = getElementFromHTML("btnTrTxt");submitButton.click();}]]></mx:Script><mx:HTML id="htmlTranslate" x="10" y="84" location="http://babelfish.altavista.com/" complete="initDomWindow(event)"/></mx:WindowedApplication>- Download this code: accesshtmlcomponentsfromflex.txt
Very useful application.
This code is great. It was very useful to me. Waiting new posts.
Hello,
I’ve got a question concerning the code. I’m trying to implement it into an application I’m writing but I cannot seem to get it working when I send a variable other than a string to the domelement. Is a string the only data possible?