473,772 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing Arrays To <select> Tag

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:

function writeSelect(nam e_group) {
var this_option;
var my_select = document.getEle mentById("namel ist");
for (var loop = 0; loop < name_group.leng th; loop++)
{
this_option = new Option();
this_option.val ue = the_array[loop];
this_option.tex t = the_array[loop]
my_select.optio ns[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 <selecttag? Or could this have been done another
way - if so what this that way?

Any response would be appreciated.

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200806/1

Jun 27 '08
12 2335
<script language="javas cript">

doselect();

function doselect()
{
try
{
var array = Array("select your value","bir","s uç
bende","yine"," cennet","zor güzel","A","yok ","insanlar","s us","sakýn
söyleme","ben", "aðla","göç ");

document.write( "<select>") ;
for(var i = 0; i < array.length; i++)
{
document.write( "<option value ="+i+">"+arr ay[i]+"</option>");
}

document.write( "</select>");
}

catch(e)
{
document.write( e.name+" : "+e.description );
}
}
</script>
Jun 27 '08 #11
Baris-C wrote:
<script language="javas cript">
<http://validator.w3.or g/#validate-by-input>
doselect();

function doselect()
This is still unnecessary and pointless. If would only make sense if the
Array object reference would be passed instead, so that the method would
become general.
{
try
{
Unnecessary. Error-prone, because not universally supported. No statement
here is going to throw an exception.
var array = Array("select your value","bir","s uç
^^^^^
Unwise choice for an identifier.
bende","yine"," cennet","zor güzel","A","yo k","insanlar"," sus","sakın
söyleme","ben" ,"ağla","göç ");
This should be passed as an argument (in the following: `a') instead.
document.write( "<select>") ;
for(var i = 0; i < array.length; i++)
{
document.write( "<option value ="+i+">"+arr ay[i]+"</option>");
}

document.write( "</select>");
}
Sigh. [psf 10.1]

function esc(s)
{
return String(s).repla ce(
/[<>&]/g,
function(m) { return "&#" + m.charCodeAt(0) + ";" });
}

var out = ['<select size="1">'];

for (var i = 0, len = a.length; i < len; i++)
{
// if Array.prototype .push() isn't available,
// augmentation needs to provide it
out.push('<opti on value="' + i + '"' + (i === 0 ? ' selected' : '')
+ '>' + esc(a[i]) + '<\/option>');
}

out.push('<\/select>');

// "\n" for pretty printing
document.write( out.join("\n")) ;
catch(e)
{
document.write( e.name+" : "+e.description );
}
If document.write( ) throws an exception (which is the only statement that
could be expected to, in Gecko's XHTML DOM), what makes you think that
another document.write( ) call could be successful then? Even more, what
makes you think this message could be helpful to the user?
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Jun 27 '08 #12
Tue, 24 Jun 2008 03:51:33 -0700 (PDT), /Baris-C/:
document.write( "<select>") ;
for(var i = 0; i < array.length; i++)
{
document.write( "<option value ="+i+">"+arr ay[i]+"</option>");
}
document.write( "</select>");
Thomas Lahn already pointed in another reply invoking
document.write( ) after load overwrites the document but
document.write( ) also doesn't work with XHTML documents:

http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

--
Stanimir
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3824
by: Andrea | last post by:
Hi, I'm trying to emulate part of our client-server application as a web site so customers can use it, and I'm stuck when it comes to re-ordering items in a list. Basically we have a list of available articles ("availableItems") and a list of articles already in an issue ("selectedItems"). What I want is to be able to move articles freely between the two lists and then on submission add them to the issue (which I can), but also move...
1
2418
by: Ang Talunin | last post by:
Hey, I wondering if it's possible to retrieve all the <option>-fields from a <select> when posting a <form> to a php file Example: I've got a form like this: <form action = phpfile.php method=post > <select name= "name">
6
2408
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets say I have values selected for all three pull down menus. When I change the first "top-level" menu I want to reset both the second and third menus to the "default" state.
4
2143
by: joiv | last post by:
I'm making a <select></select> with lots of <option></option>. It contains all possible options. Because of the length of the list, I also have an <input type="text">. This is what I wish to do: onKeyDown I want all options that don't contain (or begin with, it doesn't matter which one) the typed letters to be removed from the <select>. My problem is that I don't know of any code to find words and identify
5
8320
by: Brian Foley | last post by:
Hello, I am used to using the label tag with check boxes and radio buttons in html forms. This allows me to click on the text of the label to activate/deactivate the check box / button, rather than having to click on the (possibly small) box or button. I recently tried to assign a label to a select "drop down list", but found that when I clicked on the label text to bring it into focus, the select box was reset to the first entry. ...
0
2234
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability, architecture. Case 1: On a web page I would like to render a dropdown list
6
13024
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this is not an ASP.NET related question, but I know this group is knowledgeable and quick with responses. Thanks
4
3591
by: luftikus143 | last post by:
Hi there, I have a nasty little problem, as so often, only with IE. Here is an screenshot to better illustrate the problem. http://geodata.grid.unep.ch/screenshot13.png The map is clickable (to enable zoom in and other functions). But in this case, I would like to select a value from the drop-down list, which displays as a "drop-up" list because of not having enough space downwards. Now, when I click to select a year, IE interprets this...
4
39412
by: Man-wai Chang | last post by:
-- iTech Consulting Co., Ltd. Expert of ePOS solutions Website: http://www.itech.com.hk (IE only) Tel: (852)2325 3883 Fax: (852)2325 8288
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.