Thanks Fábio, seems like it is the best way after all.

After some googling I think I found the main shenanigan, since  not
even the native select object "add" method works so one can see the
options "array". Thing is, it is not an array, it is an HTMLCollection
object that displays like an array in the console:

http://javascript.gakaa.com/htmlcollection-description.aspx

So it behaves like an array, but it is not... I think I'll try with
the getChildren element method to see if this works instead of the
lengthy for.

Regards,

David

On Aug 30, 5:00 pm, Fábio M. Costa <[email protected]> wrote:
> try iterating it using an index and the options.length property, like:
>
> for(var i = 0, l = select.options.length; i < l; i++){
> alert(select.options[i]);
>
> }
>
> this is the best way to iterate over it.
>
> --
> Fábio Miranda Costa
> front...@globocore
> *github:* fabiomcosta
> *twitter:* @fabiomiranda
> *ramal:* 6410
>
> On Mon, Aug 30, 2010 at 7:31 PM, dukeofgaming <[email protected]>wrote:
>
>
>
> > Hi,
>
> > I'm feeling really stupid about this because I feel I might be doing
> > something wrong here. I am adding options to a select element like
> > follows:
>
> > var select = new Element('select');
>
> > select.adopt(new Element('option',{
> >  'text'  : 'text',
> >  'value' : 'value'
> > }));
>
> > Now, when I try to iterate over the elements like this, I get nothing:
>
> > for(var i in select.options){
> >  alert(i);
> > }
>
> > Furthermore, if I debug like this:
>
> > console.log(select.options);
>
> > I get an empty array in the Chrome inspector console...
>
> > BUT... if I debug like this:
>
> > console.log(select.options.length);
>
> > I get "1"... and if I do this:
>
> > console.log(select.options[0]);
>
> > I get this in the console!:
>
> > <option value=​"set">​set​</option>
>
> > The selectedIndex property also sets correctly.
>
> > console.log(select.selectedIndex); //Displays "0"
>
> > ...so it looks like I'm dealing with some sort of quanum array,
> > haha... but seriously, my head is spinning.
>
> > All I can figure out is either I'm not doing this the right way, or,
> > when a select adopts an option with mootools the options array is not
> > being modified properly... but the browser kind of tries to.
>
> > Please help, I need exactly this behavior to work: setting/iterating
> > programmatically.
>
> > Thanks in advance.
>
> > Regards,
>
> > David

Reply via email to