$('div:first').is(':first') returns false, why?

css, javascript, jquery

Solution

It has to do with your markup.

`div:last` means the last `div` in your markup.

`:last` means the last `element` in your markup.

It's probably returning true because your last element is a div.

`div:first` means the first div in your markup.

`:first` always returns the `<html>` element, as in a valid HTML doc, it's your first element.

So `$('div:first').is(':first')` should never return true in a valid HTML doc.

Problem

This looks like a bug, but I'm not sure. As far as I know, jQuerys `.is()` method will just check if the selector string matches the element you call it on (if you pass in a selector string ofc...) If you full qualify the statement into ``` $('div:first').is('div:first') ``` it will correctly return `true`, but.. here comes the facepalm for me: ``` $('div:last').is(':last') ``` will again, correctly return `true`. Check it out: http://jsfiddle.net/d6UGw/

Original source