2

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');
});
1
  • 1
    Generally the 2 codes are totally different. The first selects just 1 element having id #full-sized-content under the element $fullArea, so if that element is not contained in $fullArea, you receive an empty jquery object. While the second always returns 2 objects of #full-sized-content and #full-sized-area. Of course those ids should exist in the HTML code. Commented Jun 18, 2014 at 11:39

5 Answers 5

2

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');
Sign up to request clarification or add additional context in comments.

Comments

1

There is a difference,

Demo : Multiple selector

var $fullContent = $('#full-sized-content, #full-sized-area');

Selects both the elements - Multiple selector: $('#elem1,#elem2,...')


Demo: Parent-child

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                

Comments

1

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

Comments

1
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.

FIDDLE DEMO

Comments

0

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.

1 Comment

@AmitKumar details added you can take a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.