Results 1 to 4 of 4

Thread: select option.value to input with same number, almost works!

  1. #1
    Join Date
    Dec 2007
    Location
    Stranded in Sarasota Florida
    Posts
    75
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Question select option.value to input with same number, almost works!

    This almost works however I can't seem to start the script with i set to 0.

    Each val_# id should have it's value equal the relative number of the select menu's value.

    So in the following situation with the select option values in mind I want to have the following...
    val_1 = 1 (the select menu's first option's value)
    val_2 = 2 (the select menu's second option's value)
    val_3 = 3 (etc...)

    ...however at best the script below sets it to...
    val_1 = 2
    val_2 = 3
    val_3 = 4

    Here is what I currently have...

    <html>
    <head>
    <script>
    function my_function() {
    for(var i=1; i<document.getElementById('my_select').length; i++)
    {
    document.getElementById('val_' + i).value = document.getElementById('my_select')[i].value;
    }
    }

    function start() {
    my_function();
    }
    window.onload = start;
    </script>
    </head>

    <body>

    <a href="javascript:my_alert();">index?</a>

    <select id="my_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="6">6</option>
    <option value="7">7</option>
    </select>
    <br />
    <input id="val_1" type="text" value="" />
    <input id="val_2" type="text" value="" />
    <input id="val_3" type="text" value="" />
    <input id="val_4" type="text" value="" />
    <input id="val_5" type="text" value="" />
    <input id="val_6" type="text" value="" />
    <input id="val_7" type="text" value="" />
    </body>
    </html>

  2. #2
    Join Date
    Dec 2007
    Location
    Stranded in Sarasota Florida
    Posts
    75
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Thumbs up

    Can any one explain how I can correctly reference the usage of the child parenthesis inside of getElementById for future references? The code below works fine now...
    <html>
    <head>
    <script>
    function my_function() {

    for(var i=0; i<document.getElementById('my_select').length; i++){
    document.getElementById('val_' + (i+1)).value = document.getElementById('my_select')[i].value;
    }
    }

    function start() {
    my_function();
    }
    window.onload = start;
    </script>
    </head>

    <body>

    <a href="javascript:my_alert();">index?</a>

    <select id="my_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="6">6</option>
    <option value="7">7</option>
    </select>
    <br />
    <input id="val_1" type="text" value="" />
    <input id="val_2" type="text" value="" />
    <input id="val_3" type="text" value="" />
    <input id="val_4" type="text" value="" />
    <input id="val_5" type="text" value="" />
    <input id="val_6" type="text" value="" />
    <input id="val_7" type="text" value="" />
    </body>
    </html>

  3. #3
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    So in the following situation with the select option values in mind I want to have the following...
    val_1 = 1 (the select menu's first option's value)
    val_2 = 2 (the select menu's second option's value)
    val_3 = 3 (etc...)
    actually, the first select value is 0, like in an array. to solve this you would do this:
    Code:
    function my_function() {
    for(var i=1; i<document.getElementById('my_select').length; i++)
    {
    document.getElementById('val_' + i).value = document.getElementById('my_select')[i-1].value;
    }
    }
    Edit: Edit:
    sorry, posted at the same time.

    Edit: Edit 2:
    while looking over your code, i found an error:
    Code:
    for(var i=0; i<document.getElementById('my_select').length; i++){
    document.getElementById('val_' + (i+1)).value = document.getElementById('my_select')[i].value;
    }
    the correct way to do this would be:
    Code:
    for(var i=0; i<document.getElementById('my_select').length; i++){
    document.getElementById('val_' + (i+1)).value = document.getElementById('my_select').options[i].value;
    }
    Last edited by Master_script_maker; 05-12-2008 at 09:21 PM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  4. The Following User Says Thank You to Master_script_maker For This Useful Post:

    JAB Creations (05-12-2008)

  5. #4
    Join Date
    Dec 2007
    Location
    Stranded in Sarasota Florida
    Posts
    75
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    It's ok.

    01010100010110010101011001001101!

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
  •