Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Help with Java New Array Population!

    I have this array
    Code:
    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?
    Last edited by Dazed; 01-28-2009 at 10:07 PM. Reason: Way too confusing

  • #2
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    First you need to actually declare a variable name for the array, secondly you should place all of your declarations within it. Try:

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

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    From that point forward, you can already create a new option:
    Code:
    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:
    Code:
    <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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •