473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Easy element creation question..?

I'm trying to add <option value="4">Text</option> elements into the <select></select> tags. Please help me get it to write the option elements into the select tags.. if i'm on the right path.

Javascript Code:
Expand|Select|Wrap|Line Numbers
  1. var sb = document.getElementById('selectedfirstlast');
  2. var anOption = document.createElement('option');
  3.  
  4. anOption.innerText = res[2];
  5. anOption.value = res[3];
  6.  
  7. sb.appendChild(anOption);
  8.  
HTML Code:
[HTML]
<select id="selectedfirstlast" name="selectedfirstlast" multiple></select>
[/HTML]
Dec 13 '07 #1
3 1143
Dasty
101 Expert 100+
You are doing it well, except innerText property that is quite problematic around browsers. Repalce it with this:

Expand|Select|Wrap|Line Numbers
  1. anOption.appendChild(document.createTextNode(res[2]));
This will work everywhere for sure. Except that you are doing it really well :)
Dec 13 '07 #2
Dasty thank you so much for your help, but how can I get it to add <option> elements?

Everytime i use this code, it doesn't show anything, even with your suggestion.

i.e. everytime the following function is called, i want it to add an option box to the select tags.

Expand|Select|Wrap|Line Numbers
  1.  
  2. function addOption()
  3. {
  4. var sb = document.getElementById('selectedfirstlast');
  5. var anOption = document.createElement('option');
  6.  
  7. anOption.innerText = res[2];
  8. anOption.value = res[3];
  9.  
  10. sb.appendChild(anOption);
  11. }
  12.  
Dec 13 '07 #3
Dasty
101 Expert 100+
What is res[2] and res[3]? This code is ok except that I can not see where res array is declared. So maybe your problem is that you did not initialize values you are using.

But this code is fine for sure:

Expand|Select|Wrap|Line Numbers
  1. function addOption(value, text)
  2. {
  3. var sb = document.getElementById('selectedfirstlast');
  4. var anOption = document.createElement('option');
  5.  
  6. anOption.appendChild(document.createTextNode(text));
  7. anOption.value = value;
  8.  
  9. sb.appendChild(anOption);
  10. }
I just add local variables, because i dont know where does res array come from. Just call it like this: addOption('1', 'first');
Dec 13 '07 #4

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

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
10
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
14
by: google | last post by:
I am creating a new database for use within our company, that I'd like to make reasonably secure (short of a true server based solution). The back-end of a non-server based database seems to be...
8
by: bennett.matthew | last post by:
Hello all, This is probably an elementary (no pun intended) question, but I've spent all afternoon on it and it's driving me crazy. I have a function which dynamically adds to a table. It...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.