Skip to main content
Inspiring
June 3, 2006
Question

how to loop through attributes

  • June 3, 2006
  • 2 replies
  • 423 views
Im trying to loop through some XML and use the two attributes name and href for each button im creating. so far all I have been able to do is onfuse myself, well not totally - I can pull the first name attribute from the firstChild. But I have not been able to successfully assign it to a button. I took out my button code as it was probably wrong. Heres what Ive tried...
This topic has been closed for replies.

2 replies

_Vee_Author
Inspiring
June 8, 2006
Blemmo,
your f*n awsome. thanx. It worked great.
Reviewing the script that kept swtiching back to 0 everytime it switched nodes, would it work to have a conditional in there that told it to switch from menuHolder0_mc to next menuHolder1_mc?
The movieclip menuHolder_mc is a container for the buttons button_btn and sub_btn. Button_btn is the main navigation buttons wile sub_btn is the its sub menus.
Again thanks for the lessons. Its a big help.
Inspiring
June 3, 2006
Have a look at the attached code. It goes through the childnodes and assigns the 'name' and 'href' attribute to a button's 'text' property (may be a variable assigned to a textfield in that button) and it's onRelease function. The link is also stored in a 'link' property of that button. It assumes the buttons have indexed names: button0, button1 etc., that represent the index of the childnode, and are located in _root.
If you also want an array with the found values, uncomment the lines with 'menus'. This will add objects with a 'name' and 'link' property to the 'menus' array.

hth,
blemmo

_Vee_Author
Inspiring
June 3, 2006
Well that works well, and simple too. Thank you. I had two more questions if you don't mind.
1. If I had a btn that wasnt part of this loop, how would I assign one of the attributes (say the 'href') held in the loop to it. Im noticing that when I trace the menus array from outside the loop - it traces blank. Normally I would go:
random_btn.onRelease = function(){
mc_loader.loadClip(" http://" + menus[0], "content_mc");
}

2. Im also trying to use the submenus that are in the XML. I tried the following, but didnt have the same results as before. I put my could at the inner part of the loop with an extra childNodes to go deeper into IF they had the following setup, could I pull the subMenu in each with the following:
Inspiring
June 3, 2006
The arrays are built in an event, so before that event happens, they are undefined. After the event, you can access the arrays as usual. The trace(menus); call is before the myXML_xml.load("menu2.xml"); line, which triggers the loading, so it can't trace any values. It also wouldn't trace anything when placed directly after the load() call, because the loading will take some time until all data is loaded and the onLoad event is fired. That makes it a bit hard to use the data outside the event, because you must assert that it's loaded when used. The onLoad event is just perfect for that, so it should be done there, just after the loop through the xml nodes.
For the submenus, the attached code will place the childnode data in subMenus. The arrays must be declared outside the event, otherwise they get deleted when the event finishes.

cheers,
blemmo