faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

JavaScript: How to dynamically create a list from an array (in 1 include src file)? <SELECT><OPTION>

Sep 25th, 2004 09:48
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 23 September 2004 - 08:23 pm ------------------

JavaScript: How to dynamically create a list from an array (in 1 
include src file)? <SELECT><OPTION>


By separating your data and your program in separate files maintenance
and centralization of your data becomes easier.

---

Steps: Overview:

 1. -Save the main file below as a .html file
     (e.g. 'yourarray1.htm')

 2. -Save the separate data file with your
     array (e.g. as 'yourarray1.js')
     in the same directory

 3. -Run the main .html file in your browser
     (e.g. 'yourarray1.htm')

---
---

Steps: Worked out:

 1. -Save the main file below as a .html file
     (e.g. 'yourarray1.htm')

--- cut here: begin --------------------------------------------------

<!-- ------------------------------------------------------>
<HTML>
<!-- ------------------------------------------------------>
<!-- this procedure should be located above (else you will get an
empty list) -->
<SCRIPT>
  function PROCTextCreateSelectOptionsNew( formnameS, listnameS,
arraySA ) {
   var I = 0;
   var minI = 0;
   var maxI = arraySA.length - 1;
   //
   var s = "";
   //
   for ( I = minI; I <= maxI; I++ ) {
    s = arraySA[ I ];
    document.write( eval( 'document.forms.' + formnameS + '.' +
listnameS + '.options.options[' +  I  + '] = new Option( s, s );' ) );
   }
   //
  }
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="yourarray.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<BUTTON
  ONCLICK = ' document.forms.yourformname.yourlistname.options[
3 ].selected = true;'
>Your button caption</BUTTON>
<!-- ------------------------------------------------------>
<FORM
 NAME = "yourformname"
>

<SELECT
 NAME = "yourlistname"
>

 <SCRIPT>
   PROCTextCreateSelectOptionsNew( "yourformname", "yourlistname",
FNListCreateArrayExampleSA() );
 </SCRIPT>

</SELECT>

</FORM>
<!-- ------------------------------------------------------>
</HTML>
<!-- ------------------------------------------------------>

--- cut here: end ----------------------------------------------------

 2. -Save the separate data file with your
     array (e.g. as 'yourarray1.js')
     in the same directory

--- cut here: begin --------------------------------------------------

<!-- library: list: create: array: example -->
function FNListCreateArrayExampleSA() {
  //
  var arraySA = new Array (
  [ "Austria" ],
  [ "Belgium" ],
  [ "Bulgaria" ],
  [ "Czechia" ],
  [ "Denmark" ],
  [ "Egypt" ],
  [ "Finland" ],
  [ "France" ],
  [ "Germany" ],
  [ "Greece" ],
  [ "Hungary" ],
  [ "India" ],
  [ "Ireland" ],
  [ "Israel" ],
  [ "Italy" ],
  [ "Luxemburg" ],
  [ "Netherlands" ],
  [ "Norway" ],
  [ "Poland" ],
  [ "Portugal" ],
  [ "Russia" ],
  [ "South Africa" ],
  [ "Spain" ],
  [ "Sweden" ],
  [ "Switzerland" ],
  [ "Turkey" ],
  [ "United Kingdom" ],
  [ "USA" ]
 );
 //
 return( arraySA );
}

--- cut here: end ----------------------------------------------------

 3. -Run the main .html file in your browser
     (e.g. 'yourarray1.htm')

---
---

Internet: see also:

---

JavaScript: List: Create: Array: Loop: Dynamically: Can you give 
overview creating <SELECT><OPTION>?
http://www.faqts.com/knowledge_base/view.phtml/aid/31291/fid/178

----------------------------------------------------------------------