Processing delimited list then adding to listbox in java script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Henry

    Processing delimited list then adding to listbox in java script

    I haven't worked with java script much, I know how to do this server side by
    constant post backs to the server on a webform, but it seems like i should
    be able to do this client side also...

    What happens is a modal dialog recieves a listing of users in a semi-colon
    delimited list so its like this

    user_1;user_2;u ser3;user_4;

    what I need to do is split this up in javascript and place each user into a
    listbox on the client side, how would i go about doing that? thanks


  • Steven Cheng[MSFT]

    #2
    RE: Processing delimited list then adding to listbox in java script

    Hi Brian,

    From your description, you'd like to split a string into an array in
    javascript code and dynamically create some items of
    a html listbox according to the values in the array via javascript , yes?

    As for this problem, here are my suggestions:
    1. The string spliting can be done by the split() member function of the
    text object, for example

    var txt = "user1;user2;us er3";
    var array = txt.split(";");

    2. And as for dynamically create option items for listbox( infact it is a
    <select> html element), we can use the following code:

    //modelSelect is an <select > html element

    modelSelect.opt ions.length = 0; // Clear the popup

    modelSelect.opt ions[0] = new Option("Explore r");
    modelSelect.opt ions[1] = new Option("Mustang ");
    modelSelect.opt ions[2] = new Option("Probe") ;

    And here is a web tech article discussing this:
    #Focus on JavaScript --- Modifying Items in a Dropdown List


    In addition, here a simple demo page , you may also have a look if you feel
    anything unclear:
    =============== =============== ======
    <HTML>
    <HEAD>
    <title>jslistbo x</title>
    <meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
    <meta name="CODE_LANG UAGE" Content="C#">
    <meta name="vs_defaul tClientScript" content="JavaSc ript">
    <meta name="vs_target Schema"
    content="http://schemas.microso ft.com/intellisense/ie5">
    <script language="javas cript">
    function setUsers()
    {
    var txt = document.getEle mentById("txtUs ers").value;
    var lst = document.getEle mentById("lstUs ers");

    if(txt != null && txt.length != 0)
    {
    var arr_users = txt.split(";");
    lst.options.len gth = 0;

    var i=0;
    for(i=0;i<arr_u sers.length;i++ )
    {
    lst.options[i] = new Option(arr_user s[i]);
    }
    }
    }
    </script>
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table width="100%" align="center">
    <tr>
    <td>
    <SELECT size="5" id="lstUsers">
    <OPTION></OPTION>
    </SELECT>
    </td>
    </tr>
    <tr>
    <td><INPUT type="text" id="txtUsers"> </td>
    </tr>
    <tr>
    <td><INPUT id="btnSetUsers " type="button" value="Set Users"
    onclick="setUse rs()"></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
    =============== =============== ===============

    Hope these help. Thanks.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    Build web apps and services that run on Windows, Linux, and macOS using C#, HTML, CSS, and JavaScript. Get started for free on Windows, Linux, or macOS.



    Comment

    Working...