jQuery

Get First X Number Of Elements With jQuery

Here is a quick snippet for getting the first lot of elements from a jQuery object.

Lets say you want to get the first 5 paragraphs on a page then you will start off by getting all the paragraphs on the page.

var paragraphs = $('p');

Then you use the slice() method to return the first couple of elements.

function returnElements( $obj, $value ){
     return $obj.slice(0, $value);
}

var firstFiveParagraphs = returnElements( $('p'), 5 );
Back to top ^

Join Paulund

  • Get access to 100s of premium tutorials and downloadable content
  • Members content consists of Premium WordPress plugins, CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
  • In-depth development tutorials
  • Priority tutorial requests
  • Priority email development support
  • No ads

Here at Paulund you'll find Web Development tutorials and snippets focusing mainly on PHP, WordPress and CSS. Members will get access to premium tutorials that go into more detail about development with these languages. You will also get access to downloadable premium content based on the tutorial.

Join Paulund

Leave a Reply

Your email address will not be published. Required fields are marked *