Malformed selector fails when using $el.find(..), but not when using $(..)

Malformed selector fails when using $el.find(..), but not when using $(..)

I came upon what I think is a bug when trying to refactor some code to use a common parent element. When splitting the selector up into a common part and using .find for the specific part it suddenly failed with a Syntax error since the attribute selector is missing the closing bracket. Using just the specific part as the selector in the main jQuery function works though, as does using a compound selector. Is there a difference in how selectors are parsed in the $(..) function and in the find(..) function, and should there be?

Of course I should just update the selector to be properly formed, but I am curious as to why the two functions differ.

  1. <div class='a'>
  2.     <div>
  3.       <a href='#1'></a>
  4.     </div>
  5.   </div>
  6.   <div class='a'>
  7.     <div>
  8.       <a href='#2'></a>
  9.     </div>
  10.   </div>
  11.   <div class='a'>
  12.     <div>
  13.       <a href='#3'></a>
  14.     </div>
  15.   </div>
  1. $('.a [href="#1"').text('Found!'); // This works
  2. $('[href="#2"').text('Found!'); // This works too
  3. $('.a').find('[href="#3"').text('Found!'); // This fails

Demo here: http://jsbin.com/codiyiz/edit?html,js,output