0

I'm wanting to loop an array of text. I was going to use .each() but I don't know if there is a way to do it with that. Is it possible or is there another way I need to do it? Thanks

I dont think it's necessary but here's what I was testing it with...

jQuery.each(data.reviews, function(index, itemData) {
    var p = '"'+ itemData.review +'"';
    $("#reviews p").text(p);
});

EDIT: It is an array of JSON objects. ( I think that's how you'd say it - or maybe - JSON Object of an array of objects )

By loop I'm wanting it to eventually fade in and out of each "review" and loop so if there is 5 reviews then it would loop from 5 to 0 then go back to 5 and continue infinitely. Hope that makes it more clear.

1
  • Please see my updated answer, I think the second JSFiddle link is what you're after. Commented Dec 30, 2010 at 8:27

3 Answers 3

2
for (var i = 0; i < data.reviews.length; i++) {
   $('#reviews p').text('"' + data.reviews[i].review + '"');
}

Not sure why you'd replace the #reviews p text over and over, but native Javascript is fine for iterating arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but I can't get the text to show. I'm getting back the JSON correctly too according to firebug. pastebin.com/GKZvA299
@fwaoka: Javascript uses a 0-index array. which means an array of N elements are always numbered 0 though (N-1). Starting your loop at .length is out of bounds in the array. You need to either start at 0 and count until < .length or start at .length-1 and count to 0. (Also, get rid of the if statement in your while loop, that will get you an infinite loop).
Brad well I was wanting the loop to be infinite. So that it would continuously cycle through the array of reviews. I'm wondering now if it's not possible to do it this way. Perhaps I should make a bunch of <p>'s and just hide all the ones except the one I'm wanting to display. Although, the source would look kinda ugly I would guess with 25+ <p>'s IDK.
1

Not quite sure what you mean by "loop an array of text". Is it safe to assume data.reviews is a JSON object? if so, what you have should work, but you may want to use:

$('#reviews').append($('<p>').text('"'+itemData.review+'"'));

That would be in place of your current code in the .each block. (this is assuming you're adding a new paragraph to #reviews for each entry. Your current method keeps replacing the contents of the paragraph with the very last entry found.)

JSFiddle sample: http://jsfiddle.net/HcV3P/

JSFiddle v2 -- with rotators: http://jsfiddle.net/HcV3P/2/

7 Comments

Perfect! Thanks going to try and see what you did now. Thanks a bunch!
@fwaokda: here's a commented version: jsfiddle.net/HcV3P/3 but don't be afraid to add a comment here and get my attention.
Your code on JSFiddle worked great, but when trying it out it starts off with a double delay and then at the end it is blank for about double the time. Any ideas? Edit: Here's my file - pastebin.com/Xn3ftp1s
@fwaokda: Try adding the setInterval in to the ajaxComplete method so the reviews have been populated when it gets called.
@fwaokda: Did you ever get this fixed? If not, make a jsfiddle with what you have now and Monday I'll take a look.
|
1

I think you should do it this way

data.reviews.each(function(index) {
    //use $(this) to get the current review element
});

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.