Link to home
Start Free TrialLog in
Avatar of Scott Baldridge
Scott Baldridge

asked on

jquery conditionally populate list boxes

Hello, I have two list boxes, available and selected. I want to make sure that I remove any available options for the available if exists in the selected.

With my code below all items are put in the available and none in the selected. What am I doing wrong?

    var availableTags = [];
    var selectedTags = [];

    var jsonAvailble = '[{"id":"1","name":"One"},{"id":"2","name":"Two"},{"id":"3","name":"Three"},{"id":"4","name":"Four"}]';
    var jsonSelected = '[{"id":"4","name":"Four"}]';

    availableTags = $.parseJSON(jsonAvailble);
    selectedTags = $.parseJSON(jsonSelected);

    for (var i = 0; i < availableTags.length; ++i) {

        //console.log($.inArray(availableTags[i].id, selectedTags));

        if ($.inArray(availableTags[i].id, selectedTags) >= 0) {

            $('#lstSelectedTags').append('<option value="' + availableTags[i].id + '">' + availableTags[i].name + '</option>');
        }
        else{
            $('#lstAvailableTags').append('<option value="' + availableTags[i].id + '">' + availableTags[i].name + '</option>');
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Baldridge
Scott Baldridge

ASKER

Thanks! That worked!