javascripthtmljqueryjquery-selectorsparent

jQuery: how to find sibling from parent


If i have sth. like this

<div><input rel="first" type="radio" name="x" value="1"></div>
 …
<div><input type="radio" name="x" value="2" disabled></div>
 …
<div><input type="radio" name="x" value="3" disabled></div>
 …
<div><input type="radio" name="x" value="4"></div>

i want to find the next enabled radiobutton after rel="first" one. As i had no divs wrapped, there was a simple way to find

$('input[rel="first"]').nextAll('input:radio').not(':disabled'); 

But i cant get them, now as i had to wrap inputs with a div.

$('input[rel="first"]').parent().nextAll('input:radio').not(':disabled');

not working either. Clearly parent() is searching next Element on the level of div. But how to get inside following divs? I have tried this

$('input[rel="first"]').parent().nextAll('div ~ input:radio').not(':disabled');

but it is also not working


Solution

  • Try this:

    $('input[rel="first"]')
      .parent() //it's parent div
      .nextAll('div') //finds nextAll div
      .children('input:radio') //inside div find input with radio type
      .not(':disabled'); //ensures not disabled