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 2 of 2

Thread: selectbox

  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    selectbox

    Hi All,I am new to javascript.i want to know how can i generate selectbox dynamically using javascript.

    what i mean is
    for(int i=0;i<10;i++)
    {
    <select name="list" id="listid"><option>1</option></select>
    }
    every time i have to change the id of select tag.Any suggestions?

    Thank You

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,988
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    This should point the way:-

    Code:
    <body onload = "load_combo ('sel_planet', planets)">
    
    <select id= "sel_planet">
    </select>
    
    <script type = "text/javascript">
    
    var planets = new Array ("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto");
    
    function add_option (select_id, text) {
    var select = document.getElementById(select_id);
    select.options[select.options.length] = new Option(text);
    }
    
    function load_combo (select_id, option_array) {
    for (var i = 0; i < option_array.length; i++) {
    add_option (select_id, option_array[i]);
    }
    }
    
    </script>
    for(int i=0; i<10; i++) is incorrect syntax. Should be
    for(var i=0; i<10; i++)


    Tourist in Athens to tour guide: "You have told us something about Byzantine architecture. Would it be possible to meet some Byzants?"


  •  

    Posting Permissions

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