PDA

View Full Version : Generate an array from onchange selection



dotcomtim
12-07-2006, 05:50 PM
I have an array that I would like to dynamically generate based on what a user selects in a drop down box, is there anything on the boards that has been created alreay similar I am not search correctly on? Thanks

<select name="photoarray" onchange="">
<option value="1">1 Photo</option>
<option value="2" >2 Photos</option>
<option value="3">3 Photos</option>
<option value="4">4 Photos</option>
<option value="5">5 Photos</option>
<option value="6">6 Photos</option>
<option value="7">7 Photos</option>
<option value="8">8 Photos</option>
<option value="9">9 Photos</option>
<option value="10">10 Photos</option>
</select>

If option 4 Photos is selected in the photoarray select box would produce,
var files = new Array("photo1.jpg", "photo2.jpg", "photo3.jpg", "photo4.jpg")

And if 6 Photos is selected it would produce,
var files = new Array("photo1.jpg", "photo2.jpg", "photo3.jpg", "photo4.jpg",”photo5.jpg”,”photo6.jpg”)

coothead
12-07-2006, 07:54 PM
Hi there dotcomtim,

does this example help...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
window.onload=function() {
document.forms[0][0].onchange=function() {
files=new Array();
num=this.value;
for(c=0;c<num;c++) {
files[c]='photo'+(c+1)+'.jpg';
}
alert(files);
alert('the first image in the array is...\n\n'+files[0]);
alert('the last image in the array is...\n\n'+files[c-1]);

}
}
</script>

</head>
<body>

<form action="#">
<div>
<select name="photoarray" onchange="">
<option value="1">1 Photo</option>
<option value="2" >2 Photos</option>
<option value="3">3 Photos</option>
<option value="4">4 Photos</option>
<option value="5">5 Photos</option>
<option value="6">6 Photos</option>
<option value="7">7 Photos</option>
<option value="8">8 Photos</option>
<option value="9">9 Photos</option>
<option value="10">10 Photos</option>
</select>
</div>
</form>

</body>
</html>
coothead

dotcomtim
12-07-2006, 09:29 PM
Absolute, bang on! .. that just saved big time.. thank you very much!

coothead
12-07-2006, 09:45 PM
No problem, you're welcome. ;)