Reputation: 1036
My html looks like -
<div id="slide1">
<a href="#content1">
<img src="demo_files/images/Carousel_Slides19-26/Slide19A.jpg" alt="" class="active1"/>
</a>
<a href="#content1">
<img src="demo_files/images/Carousel_Slides19-26/Slide19B.jpg" alt=""/>
</a>
</div>
I'm able to find the first image using jQuery with -
var $active = $('#slide1 .active1');
How do I use jQuery to find the next image without using that source within the selector? (The one with src="demo_files/images/Carousel_Slides19-26/Slide19B.jpg")
I thought
$('#slide1 .active1').next()
Would work, but the next element isn't that image.
Upvotes: 0
Views: 98
Reputation: 388446
Since .active
is the img
element, it does not have a next sibling so .next()
won't work. You need to find the next sibling of the parent element(a
) of .active
like
$('#slide1 .active1').parent().next().find('img')
Upvotes: 2
Reputation: 21
First, You must make sure that only one ID in HTML.
$('#slide1 .active1').next()
is equal to
$('#slide1 .active1').eq(0).next()
Following is resolutions:
$('#slide1 img').eq(1)
$('#slide1 img')[1]
$('#slide1 .active1').parent().next().children().eq(0);
Upvotes: 2
Reputation: 18265
select all image, and use eq
to position the index.
var $active = $('#slide1 img').eq(1);
Upvotes: 2