Crays
Crays

Reputation: 2508

jquery toggling more than one div

i'm trying to make toggling div with only 1 javascript.

I tried this, the first div does what it was meant to do but the second doesn't.

Have a look.

<body>
    <div>
    <div>He
        <div>You
            <div id="Me"><a id="me">Me</a></div>
        </div>
    </div>
    <div id="This">We
    </div>
    </div>
    <div>

    <div>1
        <div>2
            <div id="Me"><a id="me">3</a></div>
        </div>
    </div>
    <div id="This">4
    </div>
    </div>
<script>
    $("#me").click(function () {
      $(this).parent().parent().parent().siblings("#This").slideToggle("slow");
    });
</script>
</body>

when i click me, we disappear, alright. But when i click 3, 4 doesn't disappear.

Upvotes: 2

Views: 143

Answers (2)

Dan Klassen
Dan Klassen

Reputation: 116

if you don't want to add classes for whatever reason you can target multiple ids at once with jQuery. You still will need unique identifiers on your elements however.

If you want to toggle multiple ids in one call you should be able to do something like:

$('#id1, #id2, #id3').slideToggle();

Upvotes: 1

Nikita Rybak
Nikita Rybak

Reputation: 67986

Ids are expected to be unique. For 'markers' you can use 'class' attribute. (not sure if this is the cause, but there is a good chance that it is)

Upvotes: 5

Related Questions