Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news.albasani.net!.POSTED!not-for-mail From: Stefan Weiss Newsgroups: comp.lang.javascript Subject: Re: Who's eaten my apple and why? Date: Thu, 27 Sep 2012 03:51:40 +0200 Organization: albasani.net Lines: 52 Message-ID: References: <37c94a7b-70f8-47f5-b5b8-2a1b3350de43@u19g2000yqo.googlegroups.com> <8dedf519-0485-431a-b46e-58707db5af27@l14g2000yqo.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net lvGqRlezpwiDyxRbgdYNftLl23eb8VLqL8p/CSSwTJkW73qyhei+C666tty3FjF3Dvdgwo1dfJEjTmR4as0HaPAg0oVEOYg87SzqoxumAabvRMumaFvCG3q+oH0vYLhM NNTP-Posting-Date: Thu, 27 Sep 2012 01:51:42 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="PaZStcW+2KH4r8EoNAu3UyegoDxbx0g8sD1kt7jqNjF98Ehr8G2CWqRsIDyLr2UpGAhxaLezlI+GzybAPG1sABkpxbrkxOwya7IRtumFLR9kapLS0yfga2MmfnkcQbVG"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 In-Reply-To: <8dedf519-0485-431a-b46e-58707db5af27@l14g2000yqo.googlegroups.com> Cancel-Lock: sha1:Z7H+FZJxmsTg17W9m6ezZdKgQFs= Xref: csiph.com comp.lang.javascript:16165 On 2012-09-27 02:46, justaguy wrote: > The following code works but I'm wondering if there's more elegant way > to do it, that is, using some sort dynamic variables inside the loop. > > case dayAfterTmr: > var sel = document.getElementById('mySel'); > var obj = []; > var Os = ['apple','orange','banana']; > for (i=0; i < Os.length; i++) { > obj[i] = document.createElement('option'); > obj[i].value = Os[i]; > obj[i].text = Os[i]; > sel.options.add(obj[i]); > } > > break; I have no idea what you mean with "dynamic variable". I can see that you're using an 'obj' array that serves no purpose, though. This will do the same job (given 'sel' and 'Os'): for (var i = 0; i < Os.length; ++i) { var opt = document.createElement("option"); opt.text = opt.value = Os[i]; sel.options.add(opt); } Note that you're assigning to the _same_ 'opt' variable on each iteration. Every time you assign it a reference to a new object, the old value is discarded. Also note that I declared the 'i' variable. I can't be sure from the snippet of code you sent, but you seem to have missed that. If you want something even shorter, you could try this: for (var i = 0; i < Os.length; ++i) { sel.options.add(new Option(Os[i], Os[i])); } Depending on your choice of standards, you may or may not want to use "new Option()". It's standard in JavaScript(tm), and documented by several other implementations, but it's not part of the W3C DOM. That said, it has worked reliably in every (scripting enabled) browser I've seen in the last 10 years. I don't hesitate to use it, but this group is a little "special", and I'm sure somebody will chime in with some fantastically interesting nitpicks. By the way, unless you're doing something unusual, it's sufficient to set the .text of an option element when it's the same as the value. - stefan