Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Emptying a View

Hi

Is there any way to empty the contents of a view - without knowing the children views objects names?

Caspar

— asked August 9th 2010 by Caspar Jespersen
  • delete
  • iphone
  • remove
  • view
0 Comments

6 Answers

  • Accepted Answer

    
    for( var c in the_view.children ) {
        the_view.remove( the_view.children[c] );
    }
    

    not tested.

    — answered August 9th 2010 by Dan Tamas
    permalink
    0 Comments
  • Tama's code didn't work quite right for me… but this did (after some aggravating debugging):

        for(var c=the_view.children.length-1;c >= 0; c-- ) {
            the_view.remove( the_view.children[c] );
        }
    
    — answered September 22nd 2010 by Gary Wong
    permalink
    0 Comments
  • Dan Tomas's answer above does not work. In the second round the length of the array will be lower that when you started the loop. Therefore eventually you will exceed the array boundary.

    try this:

    while (the_view.children !== null){
        the_view.remove(the_view.children[0]);
    };
    
    — answered July 4th 2011 by David Dehghan
    permalink
    2 Comments
    • Elegant solution, works great.

      — commented July 9th 2011 by Peter Wicks
    • while (the_view.children.length){
      the_view.remove(the_view.children[0]);
      };

      This worked for me. I am using Titanium 3.2. Thank you for the idea, saved me after 3 years.

      — commented January 30th 2014 by Shiva Kumar Avula
  • Neither works for me.

    — answered July 13th 2011 by Kristof Gruber
    permalink
    0 Comments
  • There's this method that you can use:

    view.removeAllChildren( );

    I have tested it and it's working for me.

    — answered June 4th 2014 by Vahid Hoss
    permalink
    0 Comments
  • Better:

    while(navigationView.children.length){
        navigationView.remove(navigationView.children[0]);
    };
    
    — answered February 7th 2012 by Frédéric Joiris
    permalink
    1 Comment
    • Obviously, replace navigationView by your view ;-)

      — commented February 7th 2012 by Frédéric Joiris
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.