Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DIANAL, YOUR EXAMPLE WORKS BUT THE OPTIONS DON'T HAVE VALUES.

Status
Not open for further replies.

seba070707

Programmer
Joined
Aug 6, 2001
Messages
13
Location
AR
DIANAL, YOUR EXAMPLE WORKS BUT THE OPTIONS MUST HAVE 'INTERNAL' VALUES AND THEY DON'T.

Is there any way to put the combo information like this...

<option value=&quot;USA&quot;>United States of America</option>


I suppose that with that modification, the code in the HTML page must be modified too. If it is true, please help me with that thing too.


If you're not DIANAL and want to help me anyway, I will tell you what I posted yesterday...



I POSTED...
-----------------

My question is: CAN I STORE COMBO BOX OPTIONS (AND THEIR VALUES) IN AN EXTERNAL .JS FILE????

In my site, a lot of pages will have the same combo box with the same options and values and I think that it would be better if I store all the combo information in ONE JS file. Because it will be easier to UPDATE in the future.

If it is possible, please tell me how to call the options and values that are stored in the JS file and 'carry' them to the HTML page. Please, make a small example if possible.



DIANAL ANSWERED...
-----------------

========
combo.js
========
combo1 = new Array;
combo2 = new Array;

combo1[0] = &quot;--Select country--&quot;;
combo1[1] = &quot;USA&quot;;
combo1[2] = &quot;UK&quot;;

combo2[0] = &quot;--Select access--&quot;;
combo2[1] = &quot;Web access&quot;;
combo2[2] = &quot;Email access&quot;;
combo2[3] = &quot;FTP access&quot;;
// you can extend them as you need

==========
combo.html
==========
<HTML>
<HEAD>
<TITLE> Combo </TITLE>
<script language=&quot;JavaScript&quot; src=&quot;combo.js&quot;></script>
<script language=&quot;JavaScript&quot;>
function populate(combo, cboname) {
// combo is array, cboname is string
// return proper string for a combo box
str = &quot;\n&quot;; // \n is for new line
str += &quot;<select name=\&quot;&quot; + cboname + &quot;\&quot;>\n&quot;; // \&quot; escapes quotes
for (opt=0; opt<combo.length; opt++) {
str += &quot;<option value=\&quot;&quot; + combo[opt] + &quot;\&quot;>&quot;+ combo[opt] + &quot;</option>\n&quot;;
}
str += &quot;</select>\n&quot;;
return str;
}
function testvalues(frm){
// check to see if the value scan be accessed.
// NS has problems with such function, IE is OK
// frm is the form
for (opt=0; opt < frm.elements.length-2; opt ++) {
// -2 is because I do not want values for buttons
alert(frm.elements[opt].value)
}
}
</script>
</HEAD>
<BODY>
<form name=&quot;myForm&quot; method=&quot;post&quot; onSubmit=&quot;testvalues(this);&quot;>
<script language=&quot;JavaScript&quot;>
document.write(populate(combo1,&quot;combo1&quot;));
document.write(&quot;<br>&quot;)
document.write(populate(combo2,&quot;combo2&quot;));
</script>
<br>
<input type=&quot;submit&quot; value=&quot;Submit&quot;> <input type=&quot;reset&quot; value=&quot;Reset&quot;>
</form>
</BODY>
</HTML>

-----------------
 
Hello,
They do have internal values but they are equal to shown text.
The line:
str += &quot;<option value=\&quot;&quot; + combo[opt] + &quot;\&quot;>&quot;+ combo[opt] + &quot;</option>\n&quot;;
will produce <option value=&quot;USA&quot;>USA</option>

So you will need modifications:
================
in combo.js file
================

combo1short = new Array;
combo2short = new Array;
combo1 = new Array;
combo2 = new Array;

combo1short[0] = &quot;&quot;;
combo1short[1] = &quot;USA&quot;;
combo1short[2] = &quot;UK&quot;;

combo1[0] = &quot;--Select country--&quot;;
combo1[1] = &quot;United States of America&quot;;
combo1[2] = &quot;United Kingdom&quot;;

combo2short[0] = &quot;&quot;;
combo2short[1] = &quot;Web&quot;;
combo2short[2] = &quot;Email&quot;;
combo2short[3] = &quot;FTP&quot;;

combo2[0] = &quot;--Select access--&quot;;
combo2[1] = &quot;Web access&quot;;
combo2[2] = &quot;Email access&quot;;
combo2[3] = &quot;FTP access&quot;;

// you could also use two-dimensional array if you like
// i like to use two one-dimensional arrays

==================
in combo.html file
==================

function populate(comboshort,combo, cboname) {
// ... other code
str += &quot;<option value=\&quot;&quot; + comboshort[opt] + &quot;\&quot;>&quot;+ combo[opt] + &quot;</option>\n&quot;;
// other code
}

// ...

in body:
document.write(populate(combo1short,combo1,&quot;combo1&quot;));
document.write(populate(combo2short,combo2,&quot;combo2&quot;));

Success with your task!
D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top