Reputation: 563
I know this isn't a code issue related question, but it's something I'd love to know and it may help others:
What's the difference between using this:
$(function() {
var $fullArea = $('#full-sized-area');
var $fullContent = $('#full-sized-content', $fullArea);
});
Over say this:
$(function() {
var $fullArea = $('#full-sized-area');
var $fullContent = $('#full-sized-content, #full-sized-area');
});
Upvotes: 2
Views: 54
Reputation: 73896
var $fullContent = $('#full-sized-content', $fullArea);
The above actually a context based selector. So, what is does in simple manner is:-
$fullArea // get the fullArea element
.find('#full-sized-content') // find all the descendants with this id inside it
While the code below means:
// Get both the elements with id full-sized-content & full-sized-area
var $fullContent = $('#full-sized-content, #full-sized-area');
It's like a combined selector. So, if you do the below thing for 1st code:-
$fullContent.css('color', 'red');
It will only make the color red for the element #full-sized-content
inside $fullArea
Whereas if you use the same code for the 2nd part, it will color both the full-sized-content
& full-sized-area
elements.
Upvotes: 1
Reputation: 74738
Your code:
var $fullContent = $('#full-sized-content', $fullArea);
it says find me #full-sized-content
in $fullArea
, because of the ,
. ,
makes a context for this.
This code:
var $fullContent = $('#full-sized-content, #full-sized-area');
is usually called a muliple selector selection which is a string with ,
separated elems while below
var $fullContent = $('#full-sized-content', $fullArea);
is usually finds an element in a given context which is ,
separated elems.
Upvotes: 0
Reputation: 20626
There is a difference,
var $fullContent = $('#full-sized-content, #full-sized-area');
Selects both the elements - Multiple selector: $('#elem1,#elem2,...')
var $fullContent = $('#full-sized-content', $fullArea);
In this case : #full-sized-content
has to the child of #full-sized-area
So its is equivalent to
$('#full-sized-area #full-sized-content');
Parent Child
Upvotes: 1
Reputation: 856
Both are diffent here..
$(function() {
var $fullArea = $('#full-sized-area');
var $fullContent = $('#full-sized-content', $fullArea);
});
This one select element which has id "full-sized-content" under the #full-sized-area element
$(function() {
var $fullArea = $('#full-sized-area');
var $fullContent = $('#full-sized-content, #full-sized-area');
});
This one select both element with the ID full-sized-content and full-sized-area
Upvotes: 1
Reputation: 133403
Both are quite difference.
Basically you are using Multiple Selector (“selector1, selector2, selectorN”) in the following line. So its simple
$('#full-sized-content, #full-sized-area')
Where as in line you are using context based selector, Here you are selecting element with ID full-sized-content
in childs of $fullArea
var $fullContent = $('#full-sized-content', $fullArea);
is equivalent to
var $fullContent = $('#full-sized-area').find('#full-sized-content');
Note As IDs must be unique in HTML you can simply use
var $fullContent = $('#full-sized-content');
Upvotes: 2