Beetlejuice
Beetlejuice

Reputation: 4425

jquery selecting next element from $(this) position

I have the following snipped of my page:

<div class="radio-group">
<input type="radio" name="group1" checked="checked" id="option1" value="option1"/>
<label for="option1">option1</label>
<input type="radio" name="group1" id="option2" value="option2"/>
<label for="option2">option2</label>
<input type="radio" name="group1" id="option3" value="option3"/>
<label for="option3">option3</label>
<input type="radio" name="group1" id="option4" value="option4"/>
<label for="option4">option4</label>
</div>

and this javascript code to select the hidden input after a click in the radios:

$("input[type='radio']").live("change", function () {        
    var el = this.parentNode.nextElementSibling;
    el.value = this.value;
});

I want to write the line

var el = this.parentNode.nextElementSibling;

in a jQuery way. Thanks.

Upvotes: 3

Views: 4706

Answers (4)

mreq
mreq

Reputation: 6542

use jquery's this, also note that you can filter the next selector (in case something goes wrong with your code)

var el = $(this).next('input[type="hidden"]');

Upvotes: 1

Jon
Jon

Reputation: 437734

var el = this.parentNode.nextElementSibling;

translates to

var $el = $(this).parent().next();

Upvotes: 3

bjornd
bjornd

Reputation: 22951

$("input[type='radio']").live("change", function () {        
    var el = $(this).parent().next();
    el.val(this.value);
});

Upvotes: 4

Joseph Marikle
Joseph Marikle

Reputation: 78570

var el = $(this).parent().next().find("input[type='radio']");
el.val(this.value);

very easy. :)

Edit

lol didn't pay enough attention to the markup. :P fixed.

Upvotes: 5

Related Questions