5

Hi I am noob at javascript and doing practice for improving my skills.

I made one sample project and fetched data from json using getJSON.

It worked fine but what I want is to show 3rd index data first and rest onclick of loadMore button.

like First I will have "3 list item" populated with json after that I would need every 2 li to get populated on loadMore click...here is my json array

[
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
{
"imagepath" : "sample url",
"heading" : "sample heading",
"details" : "sample details"
},
]

and here is sample code

$(document).ready(function(){
                $('#fetchit').click(function(){
                $.ajax({
                    url:"one.json",
                    cache: false,
                    dataType : "json",
                    success :function(){
                        //alert('bf c')
                        $('.hold').empty();
                        $.getJSON("one.json",function(data){
                            $.each(data ,function(i,value){
                                var list ="<li>" + "<img src'" + value.imagepath + "' alt=''/>" + "<span>" + value.heading + "</span>" + "<span>" + value.details + "</span>" 
                            $('.hold').append(list)
                            })
                        })
                    },
                    error:function(xhr,status,error){
                        alert(xhr.status)
                    }
                })
            })
        });

this code is fetching whole json at one click but i want to parse it or load it in parts on click. please help me in this using ajax getJSON or javascript. I am unable to make the logic of loadMore, I know we have to do this by some counter...

1
  • what have you tried so far? just showing the json won't encourage people to help ... in my opinion - load more would only be relevant, to save traffic in the beginning - so if your json contains already all data - why not show it right from the beginning? if your first request only retrieves the first 3 objects, then i guess, you need some server side logic to generate the json on your request. as a request parameters, you could use the .length() function to know how many entries you already have and add a second variable to tell the server how many objects you want. Commented Oct 27, 2015 at 7:16

1 Answer 1

4

Try this out:- http://jsfiddle.net/adiioo7/8erwrha2/

JS:-

var json = [{
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}, {
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}, {
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}, {
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}, {
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}, {
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}, {
    "imagepath": "sample url",
        "heading": "sample heading",
        "details": "sample details"
}];

jQuery(function ($) {
    $.each(json, function (i, value) {
        var list = "<li class='hidden' >" + "<img src'" + value.imagepath + "' alt=''/>" + "<span>" + value.heading + "</span>" + "<span>" + value.details + "</span>"
        $('.hold').append(list);
    });

    function loadMore(){
        $(".hold .hidden").slice(0,2).removeClass("hidden");
    }

    loadMore();

    $("#btnLoadMore").on("click",loadMore);        

});

HTML:-

<div class="hold"></div>
<input type="button" id="btnLoadMore" value="Load More"/>

CSS:-

.hidden {
    display:none;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.