tags:

views:

456

answers:

3

I've managed to create multiple toggle instances on my page, however the toggle links themselves need to be unique, as at the moment toggling one link toggles all the rest.

Here is my code so far:

jQuery:

$(document).ready(function() {
  $('.toggle').click(function() { 
   var id = $(this).attr('name');
   $('#module' + id).slideToggle('fast');
   $('.toggle').toggle();
   return false; 
  });
});

HTML:

<a class="toggle" name="1" href="#">- Hide</a> 
<a class="toggle hidden" name="1" href="#">+ Show</a>

<div id="module1">Toggled Content</div>

As you can see, the issue is "$('.toggle').toggle();" as it's not unique to each toggle. So toggling one link closed, closes all the others, and the same when opening. Any ideas?

+1  A: 

To toggle the element that was clicked, call $(this).toggle().

SLaks
+4  A: 

To toggle both links:

$('[name=' + id + ']').toggle();
Kobi
That worked great. Thanks so much!
HU5TL3R
@hus5tl3r: if kobi's answer helped you, consider upvoting him and accepting his answer.
marcgg
+1  A: 
$(document).ready(function() {
  $('.toggle').click(function() { 
   var id = $(this).attr('name');
   $('#module' + id).slideToggle('fast');
   $('a.toggle[name='+id+']').toggle();
   return false; 
  });
});

Seems to be what you need

Gausie