Jump to content

Newbie to javascript


ohayo85

Recommended Posts

hi guys, i'm newbie in javascript and would like to ask few question.I'm doing a drop down box like this:<select name="select"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>but i hope it can be more dynamic because the option will list on what it have in the array.what i have in mind is if i create a function option() such asvar integer = new Array("1","2","3","4","5");how could i loop the integer into drop down box? I want the drop down box will show output based on what it have inside the array.for(var i=0; i<integer.length; ++i){ what should i write here?}I hope i can get the output list what it have in array like this:<select name="select"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="5">5</option></select>if inside the array got 1, 2 only. then the option will list 1, 2 and so on. and in html <form>, what should i write ?Any help greatly appreciated.. :) ohayo85

Link to comment
Share on other sites

You can access the array values by putting [number] after the variable that has the array, replacing number with an integer value, starting with 0 to get the first value.

for (var i=0; i<integer.length; i++) {  document.write('<option value="'+integer[i]+'">'+integer[i]+'</option>');}

Link to comment
Share on other sites

document.write isn't going to work very well for this. If you put an ID on your select element like this:<select id="dropdown">Then you can add options to it like this:

  for (var i in integer)  {	newopt = document.createElement("option");	newopt.text = integer[i];	newopt.value = integer[i];	try {	  document.getElementById("dropdown").add(newopt, null);	}	catch(ex) {	  document.getElementById("dropdown").add(newopt); //IE	}  }

Link to comment
Share on other sites

First of all, thanks for the reply.hmm..sorry for asking this :) I did what both of you said like:<script>function Option(){ var integer = new Array("1","2","3"); for(var i = 0; i<integer.length;i++) { newopt = document.createElement("option"); newopt.text = integer; newopt.value = integer; try { document.getElementById("dropdown").add(newopt, null); } catch(ex){ document.getElementById("dropdown").add(newopt); //IE } }}then below is my html(correct me if i'm wrong cause i quite noob in this) :)<html><body><form> <select id="dropdown"> Option(); </select></form></body></html> but it seems like the drop down list don't have any value. Suppose it show the value 1, 2 and 3 isn't it?or the way i am doing is wrong? please help.. :mellow: Thanks in advance.ohayo85

Link to comment
Share on other sites

You need to call the function inside script tags, and also after the closing tag for the <select>

<html><body><form><select id="dropdown"></select><script type="text/javascript">Option();</script></form></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...