|
-
XML question / for loop problem?
Ok, so basically have a simple nav that needs to be populated with XML. I can trace the xml just fine, but trying to get it to poulate the textfields doesnt work.
This code works, but only for the thumb1 which is hard coded, and i dont want to have to hard code each thumb, that is why i have a for loop.
Code:
for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc.thumb1.text=bookInput.Book.author.text()[i];
trace(authorElement);
}
this is what i need, which changes thumb1 to thumb[i], but doesnt work? I know this has to be simple, just not getting it.
Code:
for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc.thumb[i].text=bookInput.Book.author.text()[i];
trace(authorElement);
}
-
Senior Member
try
nav_mc["thumb"+i].text = bookInput.Book.author.text()[i];
 moo
-
I hate being an enabler.
Code:
for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc["thumb"+i].text=bookInput.Book.author.text()[i]; //shouldn't this use authorElement?
trace(authorElement);
}
But really, using naming conventions is one of my pet peeves. It would be much better if your thumbs were already in an array. And it would be better still if nav_mc had a method to take the authorList and handle the changes itself.
Last edited by 5TonsOfFlax; 02-05-2009 at 03:55 PM.
-
yeah, thanks guys, i had tried that before posting here, but no luck, i get an error. I have tried so many things, no luck. And yes 5tonsofFlax, that can be authorElement, i think i have just been trying so many different higs i never changed it back.
Get an error with your code.
Error #1010
Here is the full code.
Code:
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("nav.xml"));
function LoadXML(e:Event):void {
xmlData=new XML(e.target.data);
ParseBooks(xmlData);
}
function ParseBooks(bookInput:XML):void {
var authorList:XMLList=bookInput.Book.author;
for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc["thumb"+i].text=authorElement;
trace(authorElement);
}
}
Last edited by GDrider77; 02-05-2009 at 04:13 PM.
-
That could be the case if you have more authors in your list than thumbs on nav_mc. Or don't have a thumb0.
-
 Originally Posted by 5TonsOfFlax
That could be the case if you have more authors in your list than thumbs on nav_mc. Or don't have a thumb0.
That was exactly the problem, no thumb0, gosh i feel stupid. Thanks guys, always helpful to have a fresh set of eyes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|