By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,258 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,258 IT Pros & Developers. It's quick & easy.

Looking for better way to stream out options for list box

Claus Mygind
100+
P: 571
In the code below I get an extra line at the top of the list of choices. I would rather have the corresponding option from the list selected, so there is not a duplicate choice of the value.

Expand|Select|Wrap|Line Numbers
  1.                 cVal  = 'Gift Count ';
  2.                 cVal += '<select ';
  3.                 cVal += 'id="gCnt'+aNames[0]+'" ' ;
  4.                 cVal += 'onchange="changeStatus(this);" ';
  5.                 cVal += 'name=gCnt"'+aNames[0]+'"> ';
  6.                 cVal += ' <option value="'+aNames[13]+'">'+aNames[13]+'</option>';
  7.                 cVal += ' <option value="0">0</option>';
  8.                 cVal += ' <option value="1">1</option> ';
  9.                 cVal += ' <option value="2">2</option> ';
  10.                 cVal += ' <option value="3">3</option> ';
  11.                 cVal += ' <option value="4">4</option> ';
  12.                 cVal += ' <option value="5">5</option> ';
  13.                 cVal += '</select> ';
  14.  
I tried inserting an inline "if conditions" like the example below but it did not work
Expand|Select|Wrap|Line Numbers
  1.                 cVal  = 'Gift Count ';
  2.                 cVal += '<select ';
  3.                 cVal += 'id="gCnt'+aNames[0]+'" ' ;
  4.                 cVal += 'onchange="changeStatus(this);" ';
  5.                 cVal += 'name=gCnt"'+aNames[0]+'"> ';
  6.                 cVal += ' <option value="0"'+(aNames[13] == 0) 'selected':'' +'>0</option>';
  7.                 cVal += ' <option value="1"'+(aNames[13] == 1) 'selected':'' +'>1</option> ';
  8.                 cVal += ' <option value="2"'+(aNames[13] == 2) 'selected':'' +'>2</option> ';
  9.                 cVal += ' <option value="3"'+(aNames[13] == 3) 'selected':'' +'>3</option> ';
  10.                 cVal += ' <option value="4"'+(aNames[13] == 4) 'selected':'' +'>4</option> ';
  11.                 cVal += ' <option value="5"'+(aNames[13] == 5) 'selected':'' +'>5</option> ';
  12.                 cVal += '</select> ';
  13.  
Aug 20 '09 #1

✓ answered by Dormilich

@Claus Mygind
then why donít you create the options in a loop (numbers → for(), arrays → Array.forEach()/for(), arbitrary → for( in ))

Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
@Claus Mygind
which one is the extra line?

which is the corresponding option?

JS: option.selected = true;
HTML: <option Ö selected>
XHTML: <option Ö selected="selected">
Aug 20 '09 #2

Claus Mygind
100+
P: 571
@Dormilich
Line #6 in my top example. Here I read the value from the database table and insert it as the first option.

That means I now have two line with the same value. The one I inserted and the corresponding line with the same value in the set of options that follow.

I see you present some options for me to try, I will have to play with that to see if I can make it work. Ideally I just want to tack on "selected" to the specific line which has a corresponding value
Aug 24 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
@Claus Mygind
then why donít you create the options in a loop (numbers → for(), arrays → Array.forEach()/for(), arbitrary → for( in ))
Aug 24 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.