views:

28

answers:

2

Hello,

I have the following html code:

<div class="Radios">
     <span class="radio">
          <input type="radio" value="1"> yes
          <input type="radio" value="0"> no
     </span>
</div>
<div class="More">hello</div>
<div class="More">hello</div>

<div class="Radios">
     <span class="radio">
          <input type="radio" value="1"> yes
          <input type="radio" value="0"> no
     </span>
</div>
<div class="More">hello</div>

<div class="Radios">
     <span class="radio">
          <input type="radio" value="1"> yes
          <input type="radio" value="0"> no
     </span>
</div>
<div class="More">hello</div>
<div class="More">hello</div>
<div class="More">hello</div>

By default all the divs with class=more should be hidden. I use $('div.More').hide()

The difficult thing is that when a user clicks in a radio with value '1' all the divs.More next siblings to div.Radios should be shown (but only the inmediate siblings, not all the divs.More).

Until now, i have the parent of an element clicked, but i cannot select the next div.More elements until the next div.Radios.

Could you give me a hand?

Best Regards.

Jose

+2  A: 

but i cannot select the next div.More elements until the next div.Radios.

Use nextUntil():

$('input[type=radio][value=1]').click(function () {
    $(this).parents('.Radios').nextUntil('.Radios').show();
}
Andy E
Wow! What an elegant solution, nice answer. I should point out that this function was introduced in jQuery 1.4. So make sure you're running the latest version of jQuery.
Wireblue
@Wireblue: Yes it is elegant as long as OP is using latest version of jquery.
Sarfraz
+1  A: 

Well first off, the radio inputs that you click on are 2 levels down from the parent you care about, ".Radios".

So you want to be able to get that parent before you do anything else, something like this:

$("[type=radio]").click(function(){
   var realParent = $(this).parents(".Radios");
});

Now that you have the parent you can easily get the .next() and .prev() sibling element:

$("[type=radio]").click(function(){
   var realParent = $(this).parents(".Radios");
   realParent.next().show();
   realParent.prev().show();
});
Luca Matteis