tags:

views:

101

answers:

3

how can i have the functionality of load() except i want to append data instead of replace. maybe i can use get() instead but i want to just extract the #posts element from the loaded data


UPDATE

when i do an alert(data) i get ...

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  <script src="jquery.infinitescroll.js"></script>
  <script>

  </script>
</head>
<body>
  <div id="info"></div>
  <div id="posts">
    <div class="post"> ... </div> 
    ...
  <ul id="pageNav" class="clearfix">
    <li><a href="page1.html">1</a></li>
    <li><a href="page2.html">2</a></li>
    <li><a href="page3.html">3</a></li>
    <li><a href="page4.html">4</a></li>
    <li><a href="page5.html">5</a></li>
    <li><a href="page3.html" class="next">next</a></li>  
  </ul>

the full code can be found @ pastebin

A: 

try use $(this), something like :

<div id="result">Hello World </div>
    <script>
    $(function() {
            $(this).load('templates/test2.html', function(result) {
                $('#result').append(result);
            });
        });
    </script>
Puaka
This would replace the contents of `document` with that page...you really don't want to do anything like this, he needs something other than `.load()` in this case.
Nick Craver
+4  A: 

There's no reason you can't extract the element you want using $.get().

$.get('test.html',function(data) {
    var posts = $(data).find('#posts');
       // If the #posts element is at the top level of the data,
       //    you'll need to use .filter() instead.
       // var posts = $(data).filter('#posts');
    $('#container').append(posts);
});

EDIT:

You perhaps didn't notice the code comments above, so I'm going to make it more explicit here.

If the #posts element is at the top of the hierarchy in data, in other words if it doesn't have a parent element, you'll need to use .filter() instead.

$.get('test.html',function(data) {
    var posts = $(data).filter('#posts');
    $('#container').append(posts);
});

EDIT:

Based on the comments below, you seem to need .filter() instead of .find().

The reason is that you're passing in an entire HTML structure. When you do that, jQuery places the direct children of the body tag as the array in the jQuery object.

jQuery's .filter() filters against only the nodes in that array. Not their children.

jQuery's .find() searches among the descendants of the nodes in the array.

Because of this, you're needing to use both. .filter() to get the correct one at the top (#posts) and .find() to get the correct descendant (.next).

$(data).filter('#posts').find('.next');

This narrows the set down to only the #posts element, then finds the .next element that is a descendant.

patrick dw
You could onlineliner it even: `$('#container').append($('#posts', data));` --- Not that your example does this, but beware of calling `$(data)`/`$('#posts',data)` multiple times in this callback! That could potentially be a whole lot of unneeded processing/memory...
gnarf
@gnarf - Actually `$("#posts", data).appendTo("#container");` would be less wasteful :)
Nick Craver
@Nick: just costs readability :)
jAndy
@Nick - Doesn't jQuery just flip it around into an `.append()`? http://github.com/jquery/jquery/blob/1.4.2/src/manipulation.js#L445
patrick dw
@jAndy - That's a matter of opinion I think..."find `#posts` in data, append it to `#container`"...to me it's much *more* readable. You think "get data, find posts in it, then find container, append posts you found earlier to it" is better somehow? :)
Nick Craver
@patrick - Yep, I was replying to gnarfs version that creates an extra jQuery object on the end there.
Nick Craver
@Nick - Yes, but it seems that jQuery creates the same jQuery object with `appendTo()` when it flips it around. Maybe I'm misunderstanding you.
patrick dw
@patrick - On a single element it's doing this: `insert[original]( this[0]);`, inserting the DOM element (original == "append"), not having that extra jQuery object in the chain, it's references, `.prevObject`, etc, it's tossed ASAP once it has the DOM element.
Nick Craver
@Nick: heh, I'd like `$(data).find('#posts').appendTo('#container');` just because of the confusing order otherwise.
jAndy
@jAndy - That version works equally well, since `$("#posts", data)` is `$(data).find("#posts")` under the covers. My point was more of "what's readable" depends on the programmer, I find all of these easy to discern. That's not to say which is best for everyone, that varies, just that the more familiar you get, other permutations/formats become more readable as well. Sit me down in front of a perl script and I'd call it unreadable ;)
Nick Craver
@Nick - Just seems that `insert[original]( this[0]);` is equivalent to `$('#container').append($('#posts', data)[0]);` since `insert` is the selector for `.appendTo(selector)` wrapped in a jQuery object.
patrick dw
i tried `alert($data.find("#posts").html());` and `alert($("#posts", data).html());` both returned null. but `alert(data)` gives the correct output
jiewmeng
@jiewmeng - Did you notice my code comment about using `.filter()`? If the element you want is at the top level, meaning it doesn't have a parent element in `data`, you'll need to use `.filter()` instead of `.find()`. I'll update my answer to make it more clear.
patrick dw
@jiewmeng - ...by the way, don't be confused by the conversation above. They are suggesting different forms of what is ultimately the same thing. It is just a discussion about practices/efficiency/readability. :o)
patrick dw
oh yes `filter()` works. but i am wondering why. when i alert data, the output is the whole HTML structure, `<html><head/><body>...` so it should be hierarchical and i shld be using find? ok i will post a more complete dump of what is output in `alert()` in the update above
jiewmeng
btw, `nextHref = $data.filter("#pageNav .next").attr("href"); alert(nextHref);` does not seem to select anything ...
jiewmeng
@jiewmeng - Yes, jQuery places the direct children of the `body` tag in the array of the jQuery object. Using the code you posted, the result is an array of 8 nodes (including text nodes), and `#posts` is one of them because it is a direct child of `body`. When you use `filter()` you are only testing against the top items in that array. This is why `.filter('#posts')` works, but `.filter('#posts .next')` doesn't. It is because `.next` is a descendant of `#posts`. You need to `.filter('#posts').find('.next')` in order to filter the set down to `#posts` and find its descendants.
patrick dw
A: 
$.get("YadaYadaYada.php", function(dat) {
   $(dat).find("body > #posts").appendTo("#container");
});
Josh Stodola
Whoops, I should have read the comments to the other answer...
Josh Stodola