I want to add three toggle functions on one page (all with the same classes).
My JavaScript code:
<script type="text/javascript">
$(document).ready( function() {
$('.toggle_container').hide();
$('.trigger').click( function() {
var trig = $(this);
if ( trig.hasClass('trigger_active') ) {
trig.next('.toggle_container').slideToggle('slow');
trig.removeClass('trigger_active');
}
else {
$('.trigger_active').next('.toggle_container').slideToggle('slow');
$('.trigger_active').removeClass('trigger_active');
trig.next('.toggle_container').slideToggle('slow');
trig.addClass('trigger_active');
};
return false;
});
});
</script>
This is my HTML code:
<div id="tourteaser">
<div id="tourteaser_teaser" class="erster">
<span class="zahlen">1</span>
<h3>headline</h3>
<p>Erstelle deine persönliche Visitenkarte und zeige deine Kunst!</p>
<a class="trigger" href="" onfocus="this.blur();"></a>
<div class="toggle_container">
<div class="trenner"></div>
<div class="tourinfos"></div>
</div>
</div>
<div id="tourteaser_teaser" class="mitte">
<span class="zahlen">2</span>
<h3>headline</h3>
<p>Erstelle deine persönliche Visitenkarte und zeige deine Kunst!</p>
<a class="trigger" href="" onfocus="this.blur();"></a>
<div class="toggle_container">
<div class="trenner"></div>
<div class="tourinfos"></div>
</div>
</div>
<div id="tourteaser_teaser" class="letzter">
<span class="zahlen">3</span>
<h3>headline</h3>
<p>Erstelle deine persönliche Visitenkarte und zeige deine Kunst!</p>
<a class="trigger" href="" onfocus="this.blur();"></a>
<div class="toggle_container">
<div class="trenner"></div>
<div class="tourinfos"></div>
</div>
</div>
</div>
By now: only one (the first) toggle reacts to this function. How do I make all three work on its own? It would be also fine if one toggle closes when another one is clicked. Is that easy possible?
EDIT: I uploaded an example of my site because I can’t get SpYk3HH’s example to work with mine. See: http://www.brayaz.de/example/example.html
You had a flaw by design, but I got it working with the following;
JS
Change in HTML
to
See
WORKING JSFIDDLE HERE!
Found one major issue, first of all, it’s NEVER a good idea to give multiple elements the same ID, consider changing
<div id="tourteaser_teaser"to something like<div id="tourteaser_teaser-x"where x is the index number of that element. Second, I see you did it for the purpose of CSS, in this case you want to give them all the same CLASS name, not the same ID. You can have many class names as you like but only one ID.I think I fixed all of your problems, here’s what I did:
<a href=""to<a href="javascript:void(0);".return falseon click commandtourteaser_teasertotourteaser_teaser-1,tourteaser_teaser-2, &tourteaser_teasertourteaser_teaserto each div that had that ID previouslyposition: absolute;and because the parent was static, all of your links were overlappingposition: relative;Please see the NEW UPDATE JSFIDDLE and tell me if that’s what you’re looking for?