/    Sign up×
Community /Pin to ProfileBookmark

Loading arrays

if i hav an array in a javascript file such as:
var blah = new Array();

blah[0] = “blah”;
blah[1] = “blahblah”;
blah[2] = “blahblahblah”;
blah[3] = “blahblahblahblah”;

is there any way that i can load values of the array (blah, blahblah, etc.) into one of those select boxes?

<select>
<option></option>
<option></option>

<option></option>
</select>

to post a comment
JavaScript

20 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzSep 22.2006 — [url=http://www.quirksmode.org/js/options.html]Dynamic options[/url]
Copy linkTweet thisAlerts:
@kimstar00authorSep 22.2006 — i read through the site u gave me, but i dont really get it ?
Copy linkTweet thisAlerts:
@DokSep 22.2006 — Do something like...

[CODE]var myOptions = new Array();
myOptions[0] = "option 1";
myOptions[1] = "option 2";
myOptions[2] = "option 3";

// Get select somehow
var mySelect = document.getElementById('mySelect');

for(var i=0; i<myOptions.length; i++) {
mySelect.options[options.length] = new Option(myOptions[i]);
}[/CODE]
Copy linkTweet thisAlerts:
@KravvitzSep 22.2006 — Perhaps you should read this too then: [url=http://www.quirksmode.org/js/forms.html]Introduction to Forms[/url]

These may help as well:

http://www.javascriptkit.com/javatutors/form1.shtml

http://www.javascriptkit.com/javatutors/combos1.shtml
Copy linkTweet thisAlerts:
@kimstar00authorSep 22.2006 — hmmm ?
Copy linkTweet thisAlerts:
@KravvitzSep 22.2006 — Did you try the code Dok posted?
Copy linkTweet thisAlerts:
@kimstar00authorSep 22.2006 — this is what i hav so far. but i think i'm doing it wrong.

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<head>
<title></title>
<link rel="stylesheet" type="text/css" href="Assign1.css"/>

<script src="AssignOne.js" type="text/JavaScript"></script>
<script type="text/javascript">
//<![CDATA[
<!-- Hide the script from non-script-friendly browsers

//Function for loading prodName array into select box
function prodNamebox(){
var mySelect = document.getElementById('mySelect');
for(var i=0; i<prodname.length; i++) {
mySelect.options[options.length] = new Option(prodname[i]);
}
}

// stop hiding the script -->
//]]>
</script>

</head>

<form>
<select onload="prodNamebox();"></select>
</form>[/CODE]
Copy linkTweet thisAlerts:
@KravvitzSep 23.2006 — The select element doesn't have an ID.

I don't think any browser supports onload events on select elements.

Where are the html and body elements?
Copy linkTweet thisAlerts:
@kimstar00authorSep 23.2006 — OK i got the function to load the array prodName (i think).

Now the problem is, i dunno how to call that into my form.. Can somebody please help me.

This is the array in my JS file
[CODE]var prodName = new Array(); //product name and model array
prodName[0] = "Home Theatre System HT400DVA";
prodName[1] = "Digital Home Theatre System SD1000W";
prodName[2] = "Portable CD Player QT180H";
prodName[3] = "Portable CD Player GX1200W";
prodName[4] = "Microwave Oven R230";
prodName[5] = "Microwave Oven R380H";
prodName[6] = "DVD Recorder DV350X";
prodName[7] = "DVD Recorder DV300X";
prodName[8] = "Personal Digital Assistant SL550";[/CODE]



This is my form
[CODE]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<script type="text/javascript">
<script src="AssignOne.js" type="text/JavaScript"></script>
//<![CDATA[
<!-- Hide the script from non-script-friendly browsers
//Function for loading prodName array into select box
function prodNamebox(){
document.write('<select id="prodnamebox" name="prodnamebox">');
for (var i=0; i<prodName.length; i++) {
document.write('<option value="'+prodname[i]+'" />'+prodname[i]);
}
document.write('</select>');
}
//stop hide incompatible browser -->
//]]>
</script>
<head>

<form>
<label for="prodName">Product Name: </label>

</form>[/CODE]
Copy linkTweet thisAlerts:
@KravvitzSep 23.2006 — document.write() in not compatible with XHTML. It only works because you're serving the XHTML as text/html, which makes browsers handle it like HTML.

When do you want the function to run? When the page loads? When the user clicks on some element?
Copy linkTweet thisAlerts:
@kimstar00authorSep 23.2006 — i want it to load as soon as the page loads
Copy linkTweet thisAlerts:
@KravvitzSep 23.2006 — So why do you want to use JavaScript? Why not just write the HTML by hand?

P.S. You can't nest <script> elements like that.
Copy linkTweet thisAlerts:
@kimstar00authorSep 23.2006 — Ofcourse i prefer writing it out on html

but this is for some homework i need to do..

so wat should i do?
Copy linkTweet thisAlerts:
@KravvitzSep 23.2006 — Ah. Then I can't do your homework for you. I've given you links to two different pages that explain how to do this. If you don't know enough to make use of them, then I wonder why your teacher gave you this assignment.
Copy linkTweet thisAlerts:
@kimstar00authorSep 23.2006 — i'm wondering y i hav to do this as well, dun even think i learnt it..
Copy linkTweet thisAlerts:
@kimstar00authorSep 23.2006 — You said that "document.write() in not compatible with XHTML. It only works because you're serving the XHTML as text/html, which makes browsers handle it like HTML."

What does that mean?
Copy linkTweet thisAlerts:
@KravvitzSep 23.2006 — Mime-types are used to tell browsers how to handle files. Each one has the top-level type and a sub-type. There are 5 common top-level types: text, image, application, video, and audio.

The text/html tells a web browser to handle the file like an HTML document. XHTML is an application of XML, so in order for XHTML to be parsed with an XML parser, it needs to be sent with an XML mime-type, which include text/xml, application/xml, and application/xhtml+xml (which is the best choice for XHTML documents, except for the problem that some older browsers and IE (as of 7) don't support it.)

XML parsers can't handle document.write() and document.writeln(). One of the rules of writing XHTML is that it must be valid and follow the rules, even if you're serving it as text/html.

More information: [url=http://juicystudio.com/article/content-negotiation.php]MIME Types and Content Negotiation[/url]
Copy linkTweet thisAlerts:
@kimstar00authorSep 23.2006 — yES!

i finally got the select box to load the info from the array.

Thanks for those links, i learnt some new things.

[CODE]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>This is just an example only</title>
<script src="AssignOne.js" type="text/JavaScript"></script>
<script type="text/javascript">
<!--hide from non-script browsers


//Function for loading prodName array into select box
function show(obj){
var index=obj.selectedIndex;
var prodName = obj.options[index].value;
var message = "You picked " + prodName;
document.getElementById("order").display.value = message;
}

// hidden scripts above -->
</script>
</head>

<body>
<form id="order">

<script type="text/javascript">
<!--hide from no-script browsers
document.write('<p><select name="prodnamebox" onChange="show(this);">');
for(i=1; i<prodName.length; i++)
{
document.write('<option value=' +prodName[i]+ '>' +prodName[i]+ '</option>');
}
document.write('</select></p>');
// hidden scripts above -->
</script>

</form>

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@KorSep 23.2006 — eeer... it is not still clear for me. Your document is an XML document, acording to:

[COLOR=DarkGreen]<?xml version="1.0" encoding="UTF-8"?>[/COLOR]

or it is an XHTML document:

[COLOR=DarkGreen]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/COLOR]


?

Somehow I think you should make up your mind.

On the other hand, you should escape your javascript code using CDATA escapes

<![CDATA[

...........

]]>
Copy linkTweet thisAlerts:
@KravvitzSep 24.2006 — Kor, XHTML is an application of XML. So it's both.

kimstar00, since you've decided to use document.write(), you should use HTML 4.01 Strict instead of XHTML 1.0 Strict.

I may as well mention that using the XML declaration forces IE6 into quirks mode (aka backwards compatibility mode), which is usually a bad thing.
×

Success!

Help @kimstar00 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...