duckmike
duckmike

Reputation: 1036

finding the next image

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

Answers (3)

Arun P Johny
Arun P Johny

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

user3440171
user3440171

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:

  1. $('#slide1 img').eq(1)
  2. $('#slide1 img')[1]
  3. $('#slide1 .active1').parent().next().children().eq(0);

Upvotes: 2

Alfred Huang
Alfred Huang

Reputation: 18265

select all image, and use eq to position the index.

var $active = $('#slide1 img').eq(1);

Upvotes: 2

Related Questions