SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help populate list script

    Hey,

    I'm just starting out in javascript. I just made this script to populate a form but, it's not working. I may have made some type of obvious mistake, but I think you can get the idea of what I'm trying to do from the code.

    HTML Code:
    <script type="text/javascript">
    
    var ouch = [
    		["pig",31],
    		["cow",28],
    		["dog",31],
    		["sheep",30],
    		["hound",31],
    		["goat",30],
    		["monkey",31],
    		["zebra",31],
    		["lion",30],
    		["tigersr",31],
    		["bears",31],
    		["ohmy",31]
    ];
    
    function populateNose() {
    
    document.forms['nosebleeder'].why.options[o] = null;
    for(i=0;i=ouch.length;i++) {
    var firstValue = [i].split(",")[0];
    var secondValue = [i].split(",")[1];
    document.form['nosebleeder'].why.options[o] = new Option('secondValue','firstValue');
    }
    }
    
    
    
    
    </script>
    </head>
    
    <body onLoad="populateNose()">
    <form name="nosebleeder">
    <h1>The Hour</h1>
    <select name="why">
    <option value="first">one</option>
    <option value="second">two</option>
    </select>
    <select name="year">                                     
    </select>
    <select name="time">                                    
    </select>
    
    </body>
    </html>
    Can someone help me out. Any advice would be great.

  2. #2
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    there were a lot of small errors in that last one...

    here is the new code, I'm getting pretty close... I think..
    HTML Code:
    <script type="text/javascript">
    
    var ouch = new Array();
    
    ouch[0]= ["pig,31"]
    ouch[1]= ["cow,28"]
    ouch[2]= ["dog,31"]
    ouch[3]= ["sheep,30"]
    ouch[4]= ["hound,31"]
    ouch[5]= ["goat,30"]
    ouch[6]= ["monkey,31"]
    ouch[7]= ["zebra,31"]
    ouch[8]= ["lion,30"]
    ouch[9]= ["tigers,31"]
    ouch[10]=["bears,31"]
    ouch[11]=["ohmy,31"]
    
    
    function populateNose() {
    
    var bleeder = document.forms['nosebleeder'].why;
    
    bleeder.options.length=0;
    for(i=0;i<ouch.length;i++) {
    var parts = split(",",ouch[i]);
    var firstValue = parts[0];
    var secondValue = parts[1];
    bleeder.options[bleeder.options.length]=new Option('secondValue','firstValue');
    }
    }
    
    
    </script>
    
    <body onLoad ="populateNose()">
    
    <form name="nosebleeder">
    <h1>The Hour</h1>
    <select name="why">
    <option value="first">one</option>
    <option value="second">two</option>
    </select>
    <select name="year">                                     
    </select>
    <select name="time">                                    
    </select>
    </form>
    currently it is saying that 'split' is not defined, but I'm quite sure I have the syntex right on that one. I think the problem is that ouch[i] has no value. So the problem is either the 'for' loop or that array, I think.

    Please, any js vets give me a hand.
    Last edited by Mojo5; Jan 8, 2007 at 07:03.

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I've fixed the array by taking out the extra set of brackets and put in semicolons, so now the array definitely works. I tested it. It is the split function.

    If anyone can point out my mistake I'd be grateful.

    Thanks

  4. #4
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fixed!
    I had the 'split' function right the first time....

    HTML Code:
    <script type="text/javascript">
    
    var ouch = new Array();
    months[0]= "pig,31";
    months[1]= "cow,28";
    months[2]= "dog,31";
    months[3]= "sheep,30";
    months[4]= "hound,31";
    months[5]= "goat,30";
    months[6]= "monkey,31";
    months[7]= "zebra,31";
    months[8]= "lion,30";
    months[9]= "tigers,31";
    months[10]= "bears,31";
    months[11]= "ohmy,31";
    
    
    function populateNose() {
    
    var bleeder = document.forms['nosebleeder'].why;
    
    bleeder.options.length=0;
    for( var i =0; i < ouch.length; i++) {
    var firstValue = ouch[i].split(",")[0];
    var secondValue = ouch[i].split(",")[1];
    bleeder.options[nosebleeder.options.length]=new Option(firstValue, secondValue);
    } 
    }
    
    
    </script>
    
    <body onLoad ="populateNose()">
    
    <form name="nosebleeder">
    <h1>The Hour</h1>
    <select name="why">
    <option value="first">one</option>
    <option value="second">two</option>
    </select>
    <select name="year">                                     
    </select>
    <select name="time">                                    
    </select>
    </form>
    
    </body>
    Thanks to everyone!


Bookmarks

Posting Permissions

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