RussellHarrower
RussellHarrower

Reputation: 6820

appendTo not working for this function yet append works

Ok, So I have over 1000 lines of jQuery code, and other functions are also using appendTo and it works.

However it seems that the following function appendTo wont work, while append does work.

I think it is because I am creating the DIV in the jquery file and the asking another function to fill that div. but am not sure if that is the reason.

here is the function that is playing up.

function getmessage(e) {
    $('#loadmessage').html("");
    $.getJSON('sys/classes/usermessage.php?task=fetch&subkey=' + e + '&userid=' + sessionparts[1], function(getmessage) {
        // get line status
        $.each(getmessage, function(i, item) {

            $('#loadmessage').append(item.subkey);
        });
    });    
};

Upvotes: 1

Views: 5353

Answers (1)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

The reason is that .appendTo() will not work in this case because your item.subkey is not a dom element. You have 2 choices.

1) Use .append() as your code is now

2) For using .appendTo() put your item.subkey in a dom node, as the following:

$.each(getmessage, function(i, item) {
    $("<span />").text(item.subkey).appendTo('#loadmessage');
});

Hope this helps. Cheers

Upvotes: 2

Related Questions