0

I have an HTML dom with three divs like this:

<div data-tab="1">
</div>
<div data-tab="2">
</div>
<div data-tab="3">
</div>

Then I gather all these divs in a jQuery object like this:

var $tabs = jQuery( '*[data-tab]' );

So now, $tabs contains all three. Thats great. However, I am trying to alter one of them, so I tried using find() to find the object I wanted to alter, but somehow, I cant find any of the objects in $tabs with find().

Example:

$tabs.find( '*[data-tab="2"]' ); simply returns []

Why isnt this working, or even better, what works? :P

1
  • .find only looks at the descendants of the selected elements. Commented Oct 26, 2015 at 13:02

2 Answers 2

2

You need to use .filter() function instead of .find():

$tabs.filter('[data-tab=2]')
Sign up to request clarification or add additional context in comments.

Comments

0

window.$tabs = jQuery('*[data-tab]');
var $filteredtabs = $tabs.filter('[data-tab=2]')
console.log($filteredtabs)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-tab="1">
</div>
<div data-tab="2">
</div>
<div data-tab="3">
</div>

Demo : http://jsfiddle.net/kishoresahas/9nxzttjp/

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.