Selecting items in a listbox

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

    Selecting items in a listbox

    Hi - is it possible to pass a comma delimeted list to a javascript
    function, which will then loop through the list, and check against a
    listboxs items, and if the value in the list, corresponds to an item in
    the listbox, the listbox item should be 'selected' - I'd like this to
    happen for a multiple select list box.

    Something along the lines of (sorry, I'm not knowledgable in JS just
    yet, but need to solve the problem):

    function selItems()
    {
    selItemsInList( '1,4,4,7,8');
    }

    selItemsInList( str)
    {
    for each checkitem in str
    {
    for each item in selectlist
    {
    if item.value == checkitem.value
    {
    selectlist.item .checked;
    }
    }
    }

    Thanks for any help,

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Mark

    #2
    Re: Selecting items in a listbox

    Hi - I've managed to get this working to an extent - I'm just not sure
    how to get a comma delimmetted list, and iterate through that - here's
    what I have so far, taking info from hard coded variables:

    function pop(inForm)
    {
    var option0 = "1" //this is the part I want to be read from a comma
    list
    var option1 = "6"
    var option2 = "3"
    for (var i=0; i < 3; i++) {
    for (var x=0; x < 3; x++)
    {
    if (inForm.lbMT.op tions[i].value==eval("o ption" + x))
    {
    alert("equal so selecting");
    inForm.lbMT.opt ions[i].selected=true;
    }
    }
    }
    }




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • Matt Kruse

      #3
      Re: Selecting items in a listbox

      "Mark" <anonymous@devd ex.com> wrote:[color=blue]
      > Hi - is it possible to pass a comma delimeted list to a javascript
      > function, which will then loop through the list, and check against a
      > listboxs items, and if the value in the list, corresponds to an item in
      > the listbox, the listbox item should be 'selected'[/color]

      You can use my general setInputValue() function on multiple select boxes,
      like this:
      setInputValue(d ocument.forms[0].mySelect,"a,d, h");

      The function, along with others, can be found in my validations lib at:


      --
      Matt Kruse
      Javascript Toolbox: http://www.mattkruse.com/javascript/


      Comment

      • Dennis M. Marks

        #4
        Re: Selecting items in a listbox

        In article <407fca29$0$206 $75868355@news. frii.net>, Mark
        <anonymous@devd ex.com> wrote:
        [color=blue]
        > Hi - I've managed to get this working to an extent - I'm just not sure
        > how to get a comma delimmetted list, and iterate through that - here's
        > what I have so far, taking info from hard coded variables:
        >
        > function pop(inForm)
        > {
        > var option0 = "1" //this is the part I want to be read from a comma
        > list
        > var option1 = "6"
        > var option2 = "3"
        > for (var i=0; i < 3; i++) {
        > for (var x=0; x < 3; x++)
        > {
        > if (inForm.lbMT.op tions[i].value==eval("o ption" + x))
        > {
        > alert("equal so selecting");
        > inForm.lbMT.opt ions[i].selected=true;
        > }[/color]
        Set an array like this:

        var optionList = ["a","b","c" , etc];
        for (x=0;x<optionLi st.length;x++) {
        if (optionList[x] == ???) dothis
        };

        --
        Dennis M. Marks

        Replace domain.invalid with dcsi.net


        -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
        http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
        -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

        Comment

        • Mark

          #5
          Re: Selecting items in a listbox

          Hi Matt - thank you for this.

          I tried the setInputValue on your site, and entered 1,3 and "1,3" - but
          only one of the select items hilited - have I entered the numbers
          incorrectly?

          Thanks again for the help,



          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Mark

            #6
            Re: Selecting items in a listbox

            Sorted - thanks for the help:

            <script language="JavaS cript">
            function pop()
            {
            var optionList = arguments;
            var ddl=document.ge tElementById("l bMT");
            for (var i=0; i < 3; i++) {
            for (var x=1;x<optionLis t.length;x++)
            {
            if (ddl.options[i].value==optionL ist[x])
            {
            ddl.options[i].selected=true;
            }
            }
            }
            }

            </script>
            </head>
            <body>
            <form>
            <select name="lbMT" id="lbMT" size="10" multiple>
            <option value="1">one</option>
            <option value="2">two</option>
            <option value="3">three </option>
            </select>
            <INPUT TYPE="button" VALUE="Populate Select List"
            onClick='pop(th is.form, 19,20,1,3,3,0)' >
            </form>
            </body>




            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...