Is there anything similar to getElementById in actionscript?
I\'m trying to make a prototype of a flash page wich gets it\'s data from a xhtml file. I want to have both
Since you said your input would be XHTML, you can do it with XPath:
import mx.xpath.XPathAPI;
var elementId:String = "flashdataTitle";
var elementPath:String = "//h1[@id'" + elementId + "']";
found_elements = XPathAPI.selectNodeList(xhtml.firstChild, elementPath);
if (found_elements.length == 1) {
trace(found_elements[0]);
}
The code example is inspired from here, where you also can find some mode detail on XPath and ActionScript.
AS3 has it's own XPath Library, the general approach would be the same.
Use XML.idMap (or XMLDocument.idMap in ActionScript 3.0) property if quering elements by id is enough. This method is probably fastest way to do this. While XPath gives you advanced quering capabilities it reduces performance. So if you need some elements having id attributes I recomend you use idMap.
Is there anything like the prototype.js function inspect() in Actionscript? I've tried testing the xpath solution but it just won't work. I've tested that the xpath is correct using scetchpad (I think that's what it's called), so I beleave theres a problem with the XML object... It seems to contain the xhtml file when viewing it in the debugger, though it seems quite chaotic, but if I could "inspect" the variables and trace them it would help locating the problem. (Thanks Tomalak, I will upvote your answer as soon as my "reputation" is high enough.)
BTW, I still want to hear other ideas.