views:

40

answers:

2

I'm trying to use jQuery's appendTo function to add data to some html that I load from another file.
Why won't this work:

$('#pop-up').load('html_elements/news.html #newsItem', function() {
     $(newsItems[$(event.target).attr('href')].title).appendTo($(this).find('.title'));
     gameUi.showPopUp();
     gameUi.setUpPopUp();
} );

and by "this" I mean this:

$(newsItems[$(event.target).attr('href')].title).appendTo($(this).find('.title'));

all of the individual parts work... newsItems[...] returns a string that is the title. and $(this).find('.title')) returns the element I'm trying to target, but when I test it nothing is appended to the h2 tag.

I'm by no means a javascript / jquery pro so any ideas are welcome. Thanks!

+1  A: 

I think what you're after here is .append() instead of .appendTo(), like this:

$(this).find('.title').append(newsItems[$(event.target).attr('href')].title);

If you're appending content like a string use .append(), otherwise it's trying to use your newsItems[x].title as a selector, and not finding any elements with it to append.

Nick Craver
yep, you beat me to answering my own question.
JoeM05
A: 

I've found that just using reqular append does the trick... I have to read up on some documentation I guess.

$(this).find('.title').append(newsItems[$(event.target).attr('href')].title);
JoeM05