tags:

views:

64

answers:

3

I have a page that displays search results and has a DOM like the following:

div.mcoupdisplay
    div.mcoup //search result 1
         div.lcoup
     div.rcoup
      div.rcoupmeta
       a.rcoupedit
       a.rcoupdelete
     div.updcoup
     div.delcoup
    div.mcoup //search result 2
     div.lcoup
     div.rcoup
      div.rcoupmeta
       a.rcoupedit
       a.rcoupdelete
     div.updcoup
     div.delcoup

I currently hide all div.updcoup and div.delcoup and add slideToggle functionality with the following jQuery:

    $('div.delcoup').hide();
 $('a.rcoupdelete').click(function() {
  $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400);
  $('div.updcoup').slideUp(400);
  $('div.crecoup').slideUp(400);
  return false;
 });

 $('div.updcoup').hide();
 $('a.rcoupedit').click(function() {
  $(this).closest('div.mcoup').find('div.updcoup').slideToggle(400);
  $('div.delcoup').slideUp(400);
  $('div.crecoup').slideUp(400);
  return false;
 });

Say div.updcoup is toggled in search result 1, then I toggle div.updcoup in search result 2. How can I slideUp the visible div.updcoup search result 1 when I toggle the div.updcoup in search result 2?

A: 

Your question isn't very clear to me, but reading just this:

My question is what chain would work best to target all visible div.updcoup and div.delcoup

You want this:

$('div.updcoup:visible, div.delcoup:visible').slideUp(400);
strager
Apologies for the ambiguity. I tried to be as clear as possible.I want to add something like your answer to:$(this).parent().parent().nextAll('div.delcoup').slideToggle(400);So all *other*, visible, div.delcoup slideUp when $(this) is toggled.
ivannovak
A: 

Do you want to remove an item from the set? If so, do this:

var thisDelcoup = $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400);
$('div.delcoup').not(thisDelcoup).slideUp(400);
$('div.updcoup').slideUp(400);
$('div.crecoup').slideUp(400);
strager
Your answer was so close! I'll the answer below.
ivannovak
A: 

I was inspired by strager's comments. His was not quite there, the final, working answer is below:

    $('div.delcoup').hide();
 $('a.rcoupdelete').click(function() {
  var thisDelcoup = $(this).closest('div.mcoup').find('div.delcoup');
  $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400);
  $('div.delcoup').not(thisDelcoup).slideUp(400);
  $('div.updcoup').slideUp(400);
  $('div.crecoup').slideUp(400);
  return false;
 });

 $('div.updcoup').hide();
 $('a.rcoupedit').click(function() {
  var thisUpdcoup = $(this).closest('div.mcoup').find('div.updcoup');
  $(this).closest('div.mcoup').find('div.updcoup').slideToggle(400);
  $('div.delcoup').slideUp(400);
  $('div.updcoup').not(thisUpdcoup).slideUp(400);
  $('div.crecoup').slideUp(400);
  return false;
 });

Thanks for your help Strager!

ivannovak
I'm not sure how much different that is from my answer. From what I see, instead of reusing thisDelcoup, you re-fetch the object.
strager
When I used your code, it didn't work. Mine does, that's how its different.
ivannovak