tags:

views:

51

answers:

2

HTML:

<div id="twitter" style="float:left;">
    <span></span>
</div>

jQuery:

        var obj = JSON.parse(data);

        $.each(obj.items, function (i, item) {
            $("span", this).html("('<p>" + item.title + "</p>')").appendTo("#twitter");
            if (i == 5) return false;
        });

I'm obviously doing something wrong, and can't seem to figure this out. I know the $.each method works, as I'm able to put alert(item.title) and get the expected result.

Basically all I want to do is, loop through the item and output:

<p> title </p>
<p> title 2 </p>

I just can't seem to figure this out

+1  A: 

Without knowing what your JSON looks like, it's difficult to say what you ought to be doing:

$(obj.items).each(function(i,o){
  $("<p>").text(o.title).appendTo("#twitter > span");
  if (i == 5) return false;
});

Or possibly even:

for (var i = 1; i <= 5; i++) {
  $("<p>").text(o[i].title).appendTo("#twitter > span");
}

In reality, it's a little odd to be putting paragraphs within span tags.

Jonathan Sampson
That first example assumes that obj.items is an array of DOM objects, which seems unlikely. The second assumes that o.items is an array, which is considerably more likely but not explicitly stated in the question (or needed by the code).
Shog9
@Shog9: The first method doesn't require them to be dom-elements. It's a not-so-commonly-used syntax, but it works with JSON objects - http://jsbin.com/ekovu3/
Jonathan Sampson
I see that you're right - assuming it doesn't look like any other valid parameter, jQuery attempts to convert the object to a jQuery object and the `each()` method is then merely a passthrough to `jquery.each()` with that new object as the target. However, this does mandate that the object be an array (or array-like object) - unlike `$.each()`, it will not do anything useful otherwise.
Shog9
@Shog9: jQuery doesn't convert it, it detects the type and handles it accordingly. You may want to check the source. Still going to leave the downvote? :)
Jonathan Sampson
I was looking at the source when I wrote that - this particular use isn't documented, so it's either source or guesses as a reference. You can step through it and find the call to jQuery.makeArray if you want, or just compare the results of `$.each({one: "blah", two: "bleak"}, function(n,v) { console.log(v) });` to `$({one: "blah", two: "bleak"}).each(function(n,v) { console.log(v) });` - note how the former loops over object properties, while the latter merely appends the object to the new jQuery "array" (and then loops over that).
Shog9
@Shog9: I'm not seeing the reference to `$.makeArray`. Which version/line are you looking at? Here are the two methods: http://pastie.org/839416 If any such event does take place, it's not the result of bad-coding on my part as my method merely forwards the object to the more commonly-known method.
Jonathan Sampson
@Jonathan: you're looking too far ahead - if it is ever to be used with `jQuery.fn.each()`, the argument must first *become part of a jQuery instance*. This happens in `jQuery.fn.init()`, via makeArray; see: http://github.com/jquery/jquery/blob/master/src/core.js#L163So while jQuery.fn.each() is itself nothing more than a simple forwarding function, the process by which it can be used for iteration over a normal array is somewhat more involved...
Shog9
@Shog9: Good points. I'll check over the code a bit later. Thanks for the valuable talk!
Jonathan Sampson
+5  A: 
   var obj = JSON.parse(data); 

    $.each(obj.items, function (i, item) { 
        $("<p>" + item.title + "</p>").appendTo("#twitter > span"); 
        if (i == 5) return false; 
    });
tvanfosson
This. Although I would also suggest that perhaps nesting <p> elements inside of a <span> isn't terribly wise...
Shog9
much thanks man
Jack Marchetti
Agree... maybe $("<hx>" + item.title + "</hx>").appendTo("#twitter");
aldux
@Shog9 -- agreed, it is a little backwards. Better to just leave the span out entirely and append it to the DIV.
tvanfosson