jQuery - first()

The first() method grabs and returns the first element inside of the selected parent element. This works well for element and class selectors since multiple matches can be found ....but not so well with ID selectors alone. If you use an ID selector, be sure to use a Class or Element selector to target the first/last element within.

For example:

  • list item 1
  • list item 2
  • list item 3
  • list item 4
  • list item 5


$("#btnDemo").click(function ()
{
    $("#example1 li").first().css({ "border-color": "red", "border-width": "3px", "border-style": "solid" });
});