Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16165
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Who's eaten my apple and why? |
| Date | 2012-09-27 03:51 +0200 |
| Organization | albasani.net |
| Message-ID | <k40bfe$d00$1@news.albasani.net> (permalink) |
| References | <37c94a7b-70f8-47f5-b5b8-2a1b3350de43@u19g2000yqo.googlegroups.com> <k403fb$o5g$1@speranza.aioe.org> <8dedf519-0485-431a-b46e-58707db5af27@l14g2000yqo.googlegroups.com> |
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
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Who's eaten my apple and why? justaguy <lichunshen84@gmail.com> - 2012-09-26 14:43 -0700
Re: Who's eaten my apple and why? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 01:35 +0200
Re: Who's eaten my apple and why? justaguy <lichunshen84@gmail.com> - 2012-09-26 17:46 -0700
Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 03:51 +0200
Re: Who's eaten my apple and why? justaguy <lichunshen84@gmail.com> - 2012-09-26 19:44 -0700
Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 22:06 +0200
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-26 17:59 -0700
Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 04:16 +0200
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-26 19:38 -0700
Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 05:44 +0200
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 09:28 -0700
Re: Who's eaten my apple and why? Tim Streater <timstreater@greenbee.net> - 2012-09-27 17:40 +0100
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 16:24 -0700
Re: Who's eaten my apple and why? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-29 20:47 +0100
Re: Who's eaten my apple and why? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 13:00 +0200
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 09:34 -0700
Re: Who's eaten my apple and why? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 19:14 +0200
Re: Who's eaten my apple and why? Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 21:40 +0200
Re: Who's eaten my apple and why? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-28 15:39 +0200
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-27 16:25 -0700
Re: Who's eaten my apple and why? Tim Streater <timstreater@greenbee.net> - 2012-09-27 08:52 +0100
Re: Who's eaten my apple and why? Gene Wirchenko <genew@ocis.net> - 2012-09-26 17:50 -0700
csiph-web