Is there any difference between
$('input.current_title', '#storePreferences').prop('disabled', false);
and
$('#storePreferences input.current_title').prop('disabled', false);
?
Is there any difference between
$('input.current_title', '#storePreferences').prop('disabled', false);
and
$('#storePreferences input.current_title').prop('disabled', false);
?
There IS a difference, and it is NOT subtle as others believe.
EDIT: Layman's example of each:
Let's break down what it selects.
First we have: Context selector http://api.jquery.com/jQuery/#jQuery-selector-context
This says: use a selector in context. http://api.jquery.com/jQuery/#jQuery-selector-context
While this form MIGHT work, it should really be:
OR
This meets the requirement for a context selector being met: "A DOM Element, Document, or jQuery to use as context".
This says: using the context, find inside that the selector. An equivalent would be:
Which is what happens internally. Find
'#storePreferences'
and in that find all the'input.current_title'
matching elements.Then we have: Descendant Selector
This is a Descendant Selector (“ancestor descendant”) http://api.jquery.com/descendant-selector/ which says: find all the
input.current_title
elements inside the#storePreferences
element. THIS IS WHERE IT GETS TRICKY! - that is EXACTLY what it does -finds ALL the
input.current_title
(anywhere), then finds those INSIDE the#storePreferences
element.Thus, we run into jQuerys' Sizzle right to left selector - so it initially finds MORE(potentially) than it needs which could be a performance hit/issue.
Thus the form of:
would perform better than the Descendant version most likely.
Yes, but it's subtle
The difference is in how the elements are selected.
is equivalent to1:
but is not equivalent to:
even though the same elements will be affected.
The reason they're not the same is that using
1: lines 194-202 in the jQuery v1.9.1 sourcefind
allows for the context to be returned to#storePreferences
whenend
is called.in the context of your question, the same elements will be modified, so there is no difference in functionality, but it's important to be aware of the broader implications of the selectors you use.