Strobe_
Strobe_

Reputation: 515

Limit results from AJAX call

I want to limit the amount of results that are being returned from this AJAX call to 5. I think I could use .slice() to do this but I'm not exactly sure what way to go about it.

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "myurl",
    dataType: "xml",
    cache: false,
    success: parsePopular
  });
});

function parsePopular(popular) {
  $(popular).find("item").each(function() {
    $("#popularArticles").append("<h1><p class='Bold'><a href='" + $(this).find("link").text() + "'>" + $(this).find("title").text() + "</a></p></h1>");
  });
}

Any ideas would be great.

Thanks.

Upvotes: 0

Views: 1396

Answers (2)

guest271314
guest271314

Reputation: 1

$(popular).find("item").slice(0, 5)

$("div").slice(0, 5).remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>

Upvotes: 2

gurvinder372
gurvinder372

Reputation: 68393

Slice and remove

$(popular).find('item').slice(5).remove();

Upvotes: 1

Related Questions