skyisred
skyisred

Reputation: 7105

JQuery multiple attributes in selection

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

Upvotes: 1

Views: 103

Answers (3)

RAKESH HOLKAR
RAKESH HOLKAR

Reputation: 2197

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

Upvotes: 3

adeneo
adeneo

Reputation: 318352

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.

Upvotes: 4

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

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));

Upvotes: 1

Related Questions