0

I was hoping not to ask this question yet since I decided to read more and do more research but at the moment I am not getting much far with it.

This is my code:

function listPosts(data) {
    var $count = data.count;
    var limitposts = 5;
    var $output = $('<ul class="posts" data-role="listview" data-filter="true">')
    $.each(data.posts,function(i, val) {
        console.log(i);
        if (i<limitposts && i>=0) {
    var $post = $('<li/>').append([$("<h3>", {html: val.title}),$("<p>", {html: val.excerpt})]).wrapInner('<a href="#devotionpost" onclick="showPost(' + val.id + ')"></a>');
    $output.append($post).appendTo('#postlist');
        i++;
   // return (i !== 4);
}});

}

HTML

<!-- Page: home -->
    <div id="home" data-role="page" data-theme="d" data-title="My first App">
        <div data-role="listview">
            <a href="#devotion" id="devotionclick" data-role="button">Display Messages</a>
        </div><!-- links -->
    </div><!-- page -->

    <div id="devotion" data-role="page" data-title="My first App">
        <div data-role="header" data-theme="a" data-position="fixed"> <h2>Devotional Messages</h2></div><!-- header -->
        <div data-theme="d" data-role="listview" id="postlist"> </div><!-- content -->
        <div class="addMorePosts">Load More Posts...</div> 
    </div><!-- page -->

<script src="http://howtodeployit.com/category/daily-devotion/?json=recentstories&callback=listPosts" type="text/javascript"></script>

I initially limited the number of Posts displayed by using the Return statement. I now changed it to For statement to see if I can increment the number of Posts displayed by the click of a button.

At this stage I am trying to figure out the best logic to use. I have tried to add another Function to achieve this but no luck. Still reading and researching but was hoping for guidance or good example

Chelseawillrecover
  • 2,596
  • 1
  • 31
  • 51
  • What are you trying to do? Are you trying to return a value and keep the function running? You can't do that in JavaScript. If you are trying to return a value from `$.each` see [here](http://stackoverflow.com/questions/3820269/return-a-value-when-using-jquery-each) – 0xcaff Oct 17 '13 at 21:32
  • All I want to achieve is I have an HTML div id 'clickme' and need to execute a click Function that would append more Posts into existing Posts – Chelseawillrecover Oct 17 '13 at 21:42

1 Answers1

0
function addMorePosts ( data, offset, amount ) {

    var $postsList = $('#postlist'),
        posts = data.slice(offset, amount);

    $.each(posts, function ( index, post ) {

        $postsList.append(
            '<li>' +
                '<h3>' + post.title + '</h3>' +
                '<p>' + 
                    post.excerpt + 
                    '<a href="#devotionpost" onclick="showPost(' + post.id + ')"></a>' +
                '</p>' +
            '</li>'
        );

    });

}

JSFiddle: http://jsfiddle.net/V4Ucv/2/

Jon Koops
  • 8,801
  • 6
  • 29
  • 51