uzay95
uzay95

Reputation: 16622

How to select second element with JQUERY when clicked on image?

The code below working for only first div. I can't display other divs when i clicked on the other "img"s. Do you know a method to reach second (next) element?

$(document).ready(function() {

        $('.imgOpenClose').click(function() {
            if ($(this).attr("src") == "images/openTree.gif") {
                $(this).attr("src", "images/closeTree.gif");
                $(this).parent().find('div').eq(0).show();
            }
            else {
                $(this).attr("src", "images/openTree.gif");
                $(this).parent().find('div').eq(0).hide();
            }
        });
});

Wanna open table when i clicked on plus images

And wanna see the table inside this div

Upvotes: 2

Views: 7343

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

Since you have a break element following the image, you'll need to use nextAll and select the first DIV instead of simply using next with a filter as in my original answer.

$('.imgOpenClose').click( function() {
    $(this).nextAll('div:first').toggle();
});

Upvotes: 2

Tomas Aschan
Tomas Aschan

Reputation: 60564

If you have several images with the same class-tag, you can still do it without having to append a unique id:

$(function() {
    $('.imgOpenClose').click(function() {
        $(this).next('div').children('table').toggle();
    });
});

Upvotes: 1

Related Questions