PDA

View Full Version : Help with Java New Array Population!



Dazed
01-28-2009, 07:11 PM
I have this array


new Array(new Array(""), new Array("Cruises"), new Array("Honeymoons"), new Array("Tours/Land Packages")
),

I need it to populate a <select> option list on page load. ie:
<option></option>
<option>Cruises</option>
<option>Honeymoons</option>
<option>Tours/Land Packages</option>

Any ideas?

mattyod
01-29-2009, 10:27 AM
First you need to actually declare a variable name for the array, secondly you should place all of your declarations within it. Try:



var myArray = new Array('Cruises', 'Honeymoons', 'Tours/Land Packages');

rangana
01-29-2009, 10:37 AM
From that point forward, you can already create a new option:


var myArray = new Array('','Cruises', 'Honeymoons', 'Tours/Land Packages');
for(var i = 0; i < myArray.length ; i++){
var selEl = document.getElementById('select_elements_id');
selEl.options[selEl.options.length] = new Option(myArray[i],myArray[i]);
}



See it working fine here:


<script type="text/javascript">
window.onload=function(){
var myArray = new Array('','Cruises', 'Honeymoons', 'Tours/Land Packages');
for(var i = 0; i < myArray.length ; i++){
var selEl = document.getElementById('base');
selEl.options[selEl.options.length] = new Option(myArray[i],myArray[i]);
}
}
</script>
<select id="base"></select>


Hope that helps.

Philip M
01-29-2009, 12:18 PM
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.