Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Writing Arrays To <select> Tag...
Old 06-21-2008, 03:19 AM Writing Arrays To <select> Tag...
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Hello everyone.
I'm currently learning Javascript and doing a few exercises.
One problem I'm working on takes an array of names from an xml file using Ajax and writes it to <select> <options> tags.
This is the code they use:
Code:
function writeSelect (name_group) {
   var this_option; 
   var my_select = document.getElementById("namelist");
   for (var loop = 0; loop < name_group.length; loop++)
      {
        this_option = new Option( );
        this_option.value = the_array[loop];
        this_option.text = the_array[loop]
        my_select.options[loop] = this_option;
      }
}
The parameter for writeSelect() function contains the array of names and the id "namelist" is the id for the select tag in the form element.
This is my first time seeing the new Option() function. Is this the standard for writing arrays to the <select> tag? Or could this have been done another way - if so what this that way?
Any response would be appreciated.
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-21-2008, 07:26 AM Re: Writing Arrays To <select> Tag...
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
http://www.comptechdoc.org/independe...avaoption.html
It's a child of the Object base class, which represent an option in a select.

Personally, in those days of DOM scripting, I find this approach a bit "barbaric"...
I prefer go the DOM way:
Code:
var my_select = document.getElementById("namelist");

o=document.createElement('option');
o.value=val;
o.text=txt;

my_select.appendChild(o);
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-21-2008, 12:39 PM Re: Writing Arrays To <select> Tag...
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
This is awesome!!!
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-21-2008, 01:12 PM Re: Writing Arrays To <select> Tag...
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Quote:
Originally Posted by tripy View Post
Personally, in those days of DOM scripting, I find this approach a bit "barbaric"...
I prefer go the DOM way:
Code:
var my_select = document.getElementById("namelist");
 
o=document.createElement('option');
o.value=val;
o.text=txt;
 
my_select.appendChild(o);
Thanks. I really appreciate the response. I'm new to this stuff and I'm reading this book to learn it, but I figured there had to be another way to do it. When you're new to a language, sometimes you get upset that you didn't think of it the same way the book recommends. I'll get over it.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Old 06-21-2008, 03:20 PM Re: Writing Arrays To <select> Tag...
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
When you're new to a language, sometimes you get upset that you didn't think of it the same way the book recommends. I'll get over it.
I know how you feel. But don't beat yourself over it.

Simply remember that there are multiple path to achieve the same goal.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-21-2008, 03:48 PM Re: Writing Arrays To <select> Tag...
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
This is awesome!!!
Yup, my avatar is Arthur, from sheldon comic. Read it every weeks morning !
The source of the avatar: http://www.sheldoncomics.com/archive/080129.html and http://www.sheldoncomics.com/archive/080130.html

And some good one I've found looking for those 2 up there:

http://www.sheldoncomics.com/archive/080210.html
http://www.sheldoncomics.com/archive/080217.html
http://www.sheldoncomics.com/archive/080303.html

http://www.sheldoncomics.com/archive/080304.html and http://www.sheldoncomics.com/archive/080305.html and http://www.sheldoncomics.com/archive/080306.html

http://www.sheldoncomics.com/archive/080311.html
http://www.sheldoncomics.com/archive/080317.html
http://www.sheldoncomics.com/archive/080513.html
http://www.sheldoncomics.com/archive/080519.html
http://www.sheldoncomics.com/archive/080523.html
http://www.sheldoncomics.com/archive/080529.html
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Writing Arrays To <select> Tag...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20793 seconds with 12 queries