3

I have two comboboxes that I need to populate with the same information when another combobox data is changed.

However, when I use the .append() method, it's apparent that it only executes the latest append call. Here is the code:

    $('.sc-days').change(function () {
        var scedo = $('.sc-edo');
        var sclpo = $('.sc-lpo');
        var selected = $(".sc-days option:selected");
        scedo.append(selected);
        sclpo.append(selected);
    });

When this is run, only 'sclpo' will be populated. You can see the results here. http://jsfiddle.net/DF42s/

5
  • 1
    An element can't be in two places at once. Both calls are made. The first moves it from its current location to the scedo element, and the second moves it to the sclpo element Commented Jun 16, 2014 at 3:31
  • I just find it odd that it chooses the second drop down instead of the first. Commented Jun 16, 2014 at 3:32
  • 1
    It isn't odd. That's the final location to which you moved the selected element. Commented Jun 16, 2014 at 3:33
  • I thought it would work as you can do this sort of assignment in many other languages. Commented Jun 16, 2014 at 3:38
  • The DOM is a tree structure of objects. When you append a particular node, it's removed from its current location and relocated to the new location. So it's not really a language construct. It's just the structure of objects that is created from your HTML markup, and any given node in the structure can only exist in one location in the tree. Commented Jun 16, 2014 at 3:42

1 Answer 1

5

Assuming you intended to append copies of the original, do this:

$('.sc-days').change(function () {
    var selected = $(".sc-days option:selected");
    $('.sc-edo').append(selected.clone());
    $('.sc-lpo').append(selected.clone());
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.