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!

STORING COMBO BOX 'OPTIONS AND THEIR VALUES' IN AN EXTERNAL .JS FILE.

Status
Not open for further replies.

seba070707

Programmer
Joined
Aug 6, 2001
Messages
13
Location
AR
Hello!! I am Seba from Argentina.

You answered all my questions! THANKS A LOT TO EVERYBODY !!!

I am building a big web site, that's why I posted a lot of questions and I'll post more!
My question now 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.

THANKS AGAIN !!!!!!!
 
Yes, it is possible.
========
combo.js
========
combo1 = new Array;
combo2 = new Array;

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

combo2[0] = "--Select access--";
combo2[1] = "Web access";
combo2[2] = "Email access";
combo2[3] = "FTP access";
// 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>
 
seba, i think u wuld better use server side includes:
<!--#include file=&quot;combo.shtml&quot;--> for further info check thread215-101852
or post in perl, php or cgi forums of this GREAT site
but if u wanna do it via javascript, i think dianal wrote a good tip for ya.. regards, vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top