Reputation: 2049
I would like to select an element after I selected more elements. I think it should be something like this, but this isn't working because .is()
returns true
or false
.
Html:
<div class="bar a"></div>
<div class="bar b"></div>
jQuery:
var $el = $('.bar');
$el.addClass('active');
$el.is('.a').addClass('icon');
I could loop through $el
but I hope there might be a easier/faster way.
Using .eq(1)
is not an option as I don't know the order of the elements.
Upvotes: 0
Views: 46
Reputation: 135
It seems like you need to add icon class
on elements with class a
which is simple.
$('.bar').addClass('active')
$('.a').addClass('icon')
Upvotes: 0
Reputation: 23859
You need to use filter()
to extract the elements which have .a
assigned:
var $el = $('.bar');
$el.addClass('active');
$el.filter('.a').addClass('icon');
.active {
background-color: yellow;
}
.icon {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bar a">bar a</div>
<div class="bar b">bar b</div>
Upvotes: 2