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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with select multiple

    hi

    i amm trying to do a javascript that needs to move items between 2 lists
    I am catering for a multiple select here. However, even though the script works for a single

    select, it doesnt work for multiple. Can someone help?

    for single select:
    Code:
    var selectedList;
    var availableList;
    function createListObjects(){
        availableList = document.getElementById("availableOptions");
        selectedList = document.getElementById("selectedOptions");
    }
    
    function addAttribute(){
        var selIndex = selectedList.selectedIndex;
        if(selIndex < 0)
            return; 
        availableList.appendChild(selectedList.options.item(selIndex))
        selectNone(selectedList,availableList);
        setSize(availableList,selectedList);
    }
    for multiple select
    Code:
    function addAttribute(){
        
        for (int i=0; i<selectedList.length; i++){
        
        var addIndex[i] = availableList.selectedIndex[i];
    
        if(addIndex < 0)
            return;
    
        selectedList.appendChild(availableList.options.item(addIndex[i]));
        }
        selectNone(selectedList,availableList);
        setSize(selectedList,availableList);
    }

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    selectedIndex is the index number of the first selected option.

    You need to loop through the options and check their selected property.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function addAttribute(){
    
        for (i=0; i<availableList.length; i++){
            if(availableList.options[i].selected.length==0)
              return;
    
            if (availableList.options[i].selected)
             selectedList.appendChild(availableList.options[i]);
            }
        selectNone(selectedList,availableList);
        setSize(selectedList,availableList);
    }
    Do you mean something like this? i tried that and it still selects only the first option that is selected.

    Appreciate your help.

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The selected property is boolean (true/false). It doesn't have a length property.

    Remove this:
    Code:
    if(availableList.options[i].selected.length==0)
      return;
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for ur prompt reply even after removing this still doesnt work

    I notice something weird which is for example. if i select 4 options (a, b,c d), then only 2 (a,c ) will be appended. If i select e.g. 7 then 4 will be added (a,c,e,g)

    In fact the problem is such that it works when i select non-consecutive options. e.g. a, d, f but it doesnt work when i select e.g. a, b
    Last edited by newbie2006; 07-28-2006 at 04:30 AM.

  • #6
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh! The problem is that when you remove one the number changes and you increment it as well. Try looping through the options backwards.

    Code:
    for (i=availableList.length-1; i>=0; i--){
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #7
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks so much. it's working now


  •  

    Posting Permissions

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