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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populate dropdown box

Status
Not open for further replies.

tanneddevil

Programmer
Apr 20, 2004
18
CA
i know its not that hard but..what can i say..

i want to use an array of names to populate a drop down list embedded in the html code


anyone have any suggestions on how to go about doing this
i know its not the full code ..but its just for a rough idea
thanks


example:
<head>
var cat = new Array("Printers","Telephone","Laptops","Email")
</head>

<body>
<body>
<form method="post" enctype="text/plain">
<table border=0>
<tr valign="top">
<script type="text/javascript">
<select name="category">
for(i=0; i< cat.length; i++;){
document.forms[0].category.option = cat
}
</select>
</script>
</body>
 
here's two ways:
Code:
<body>
   <form method="post" enctype="text/plain">
      <table border=0>
         <tr valign="top">
         
         <select name="category">
         
         <script type="text/javascript">
          var cat = new Array("Printers","Telephone","Laptops","Email");
            
          for(i=0; i< cat.length; i++){
            document.write('<option>' + cat[i] + '</option>');
          }
            
         </script>
         
         </select>
         
</body>

Code:
<head>
  <script type="text/javascript">
  var cat = new Array("Printers","Telephone","Laptops","Email");
  
  function populate(o) {
    for(i=0; i< cat.length; i++){
      o.options[o.options.length] = new Option(cat[i]);
    }
  }
  </script>
</head>

<body onload="populate(document.forms[0].category);">
   <form method="post" enctype="text/plain">
      <table border=0>
         <tr valign="top">
         
         <select name="category"></select>
         
</body>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
try this:

<html>
<head>
<script language="Javascript">
function populateCategory(){
var cat = new Array(3);
cat[0] = "Printers";
cat[1] = "Telephone";
cat[2] = "Laptops";
cat[3] = "Email";
for(var x=0; x < cat.length; x++){
document.f.category.options[x] = new Option(cat[x]);
}
document.f.category.options[0].selected = true;
}
</script>

</head>

<body onload="populateCategory();">
<form name="f" method="post" enctype="text/plain">
<table border=0>
<tr valign="top">
<select name="category">
</select>
</form>
</body>
</html>

"Behind every great fortune there lies a great crime", Honore De Balzac
 
oh well, once again Jeff beat me to it.

"Behind every great fortune there lies a great crime", Honore De Balzac
 
thank you..
i will use the second one ..
is is possible to read it from a file
then populate the list??
 
core js has no file system access.

xml is possible, but no simple cross-browser way exists to read xml...you'd have to code for IE and Moz, and i'm not sure what is supported for opera or anything on mac.

an easier way if you have server-side script available like php or asp would be to write the array from a file server-side

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
unfortunately..i dont have access to the server..
i am doing this as part of a help desk system, at my work. I know but, in order for me to get access to the server it becomes political.

I have no resources right now. and im trying to get this done fast. i need one more favour and its simple..

i need a two radio buttons..

one saying use default email.
the other saying reply to this email
when the user click on reply to this mail
the text box then becomes active in other words
when the default is set the textbox cannot become active

thanks in advance
 
like so?
Code:
<form>
	<input
		type="radio"
		name="email"
		onchange="this.form.text.disabled = this.checked;"
		checked="checked" />
	use default email
	<br />
	<input
		type="radio"
		name="email"
		onchange="this.form.text.disabled = !this.checked;" />
	reply to this email
	<br />
	<textarea name="text" disabled="disabled"></textarea>
</form>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top