# How To : only attachMovie if having childNodes

**URL:** <https://forum.kirupa.com/t/how-to-only-attachmovie-if-having-childnodes/260573>\
**Category:** flash\
**Created:** [May 16, 2008, 9:43pm UTC](https://forum.kirupa.com/t/how-to-only-attachmovie-if-having-childnodes/260573 "2008-05-16T21:43:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beebs](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Beebs](https://forum.kirupa.com/u/Beebs)\
**Post date:** [May 16, 2008, 9:43pm UTC](https://forum.kirupa.com/t/how-to-only-attachmovie-if-having-childnodes/260573/1 "2008-05-16T21:43:24Z")

</div>

I am trying to build xml menu. Some nodes have childNodes some don’t. I have a problem in my array loop : the movieclip that I use to create subMenu attached to the entire array. How can I make that the movieclip that I use for subMenu only attach to the nodes that having childNodes?

Here’s the code :

```auto

function buildingMenu(){	
	
	buttonArray = myXml.firstChild.childNodes;
	for (var i=0;i<buttonArray.length;i++)
	{
		var k = this.attachMovie("menuItem", "menuItem"+i, i);		
		mainMenu = buttonArray*;
		k._alpha = 100;
		k._x = Math.round(0 + ((k._width+4)*i));
		k.id = mainMenu.attributes.id;
        k.nameTxt.text = k.id;	
		k._y = 0;
		
		//For sub menu
		subbuttonArray = myXml.firstChild.childNodes*.childNodes;
		//subContain = this.createEmptyMovieClip("subContain"+i, i+1000);
		subContain = this.createEmptyMovieClip("subContain"+i, i+1000);
		
		for(n = 0; n<=subbuttonArray.length-1; n++){
			//trace(subbuttonArray[n])
			subMenu = subbuttonArray[n];
			var m =this.subContain.attachMovie( "sub_menuItem", "sub_menuItem"+n, n+1000);
			m.sub_nameTxt.text = subMenu.attributes.id;
			m._x = Math.round(0 + ((m._width+4)*i));
			m._y = (k._y - k.box._height)-(m.box._height*n );
			m._visible= true;
			m.onRelease = function(){
            getURL(this.itemUrl, "_blank");
         }
      }//END loop FOR subbuttonArray

```

Thanks for having a look

---

<div class="post-metadata">

**Author:** ![natronp](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@natronp](https://forum.kirupa.com/u/natronp)\
**Post date:** [May 23, 2008, 5:15pm UTC](https://forum.kirupa.com/t/how-to-only-attachmovie-if-having-childnodes/260573/2 "2008-05-23T17:15:30Z")

</div>

can’t you simply check that array length and use a conditional for your loop (trigger it to loop through only if array has a length greater than zero)?

something like this:

```auto

var subCount:Number = mySubArray.length;
if(subCount > 0){
//loop through sub items code here
};

```

does that help?

---

<div class="post-metadata">

**Author:** ![Beebs](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Beebs](https://forum.kirupa.com/u/Beebs)\
**Post date:** [May 24, 2008, 11:02am UTC](https://forum.kirupa.com/t/how-to-only-attachmovie-if-having-childnodes/260573/3 "2008-05-24T11:02:21Z")

</div>

Yes. It helps. Thanks a lot
