kovalevsky
kovalevsky

Reputation: 127

How to choose the elements from the jQuery object

I'm sorry for stupid title.

Problem: I need to select only .special element from $elements variable.

<div class="element"></div>
<div class="element special"></div>
<div class="element"></div>

Jquery:

var $elements = $('.element'),
    $special  = $element.find('.special'); // Not working.

UPD: Please, don't write something like "$('.element.special')". It's wrong for my case.

Upvotes: 0

Views: 93

Answers (3)

Jignesh Patel
Jignesh Patel

Reputation: 447

< div class="element">bbb< /div>
< div class="element special">aaa< /div>
< div class="element">aaa< /div>

var $special = $('.element.special');
$special.html("test");

Upvotes: 3

leon
leon

Reputation: 143

you can use

var $special = $('.element.special');

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388366

You need to use .filter()

var $elements = $('.element'),
    $special  = $element.filter('.special');

Since the element you are targeting is a member of the $element set, you need to use .filter(), .find() is used to find descendant members of the elements in the calling set

Upvotes: 9

Related Questions