0

I have a view with a list of a content type and using drupal behaviors and jquery toggle, I trying to toggle some content when the user clicks a div. The only problem is that it's affecting all the divs and they're all toggling open, I'd just like the one they clicked to open.

Here is my code:

(function ($) {
 Drupal.behaviors.myToggle = {
  attach: function(context, settings) {
  $('.views-row').find('.views-field-field-mp3').hide();

    $('.mp3-toggle', context).click(function() {
     $('.views-field-field-mp3').slideToggle();
    });
  }
 };
})(jQuery);

I've tried return false and a couple of other things, though none seem to work.

Any help would be much appreciated.

4
  • Where in your document is .mp3-toggle in relation to .views-field-field-mp3? Commented Feb 21, 2015 at 17:07
  • 1
    Should be able to change $('.views-field-field-mp3').slideToggle(); to $(this).parents('.views-row').find('.views-field-field-mp3').slideToggle(); if .mp3-toggle is within the views row Commented Feb 21, 2015 at 17:10
  • .mp3-toggle is within a views-row but not the same, so it should be in the same views row?
    – th0ward
    Commented Feb 21, 2015 at 18:29
  • Thanks that appears to work now! Submit your comment as the answer and I'll mark as the correct an answer.
    – th0ward
    Commented Feb 21, 2015 at 18:45

1 Answer 1

1

You'll need to make use of the this keyword inside your click function:

 Drupal.behaviors.myToggle = {
   attach: function(context, settings) {
     $('.views-row').find('.views-field-field-mp3').hide();

     $('.mp3-toggle', context).click(function() {
       $(this).parents('.views-row').find('.views-field-field-mp3').slideToggle();
     });
   }
 };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.