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.

  • Where in your document is .mp3-toggle in relation to .views-field-field-mp3? – Patrick Ryan Feb 21 '15 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 – Patrick Ryan Feb 21 '15 at 17:10
  • .mp3-toggle is within a views-row but not the same, so it should be in the same views row? – th0ward Feb 21 '15 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 Feb 21 '15 at 18:45
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, privacy policy and cookie policy

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