1

I stumbled upon this form of selector. Notice the quotes, its two attributes.

$('#item1','#item2')

It seems to return only first element, which is different from $('#item1, #item2') result. I couldn't find any documentation on what exactly this does. Can somebody explain this or link to documentation with examples please

3 Answers 3

4

It's called context, and it's the same as find(), so this:

$('#item1','#item2')

would equal :

$('#item2').find('#item1');

in other words, it searched inside #item2 for an element with the ID #item1

To select both elements with ID's #item1 and #item2, you would do:

$('#item1, #item2')

notice the difference in quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

Basically it means search #item1 in #item2.
@Derek - Yes, search for a child of #item2 that matched the ID #item1.
3

Selector in Jquery $(param) supports single string parameter and then it split parameter string and then do work for selecting element..

$('#item1','#item2') //treat first one param

$('#item1,#item2') //treat one param and splits passed string and will select both

Comments

1

You can specify any number of selectors to combine into a single result.
This multiple expression combinator is an efficient way to select disparate elements.

multiple-selector
multiple-selector-2

var list = $("div,p,span").map(function () {
  return this.tagName;
}).get().join(", ");
$("b").append(document.createTextNode(list));

Comments

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.