Reputation: 69
Here is my HTML with one of several radio buttons:
<div>
<input type="radio" value="3"> <label>Some random text.</label>
<div class="reason">
<p>Please state reason:</p>
<textarea id="reason_3" size="60">Reason stated.</textarea>
</div>
</div>
And here is my jQuery that doesn't work:
var option = 3;
var reason = $("input[value='" + option + "']").next(".reason").has("textarea").val();
My goal is to put the content of the textarea into the variable reason
. My jQuery code somehow doesn't achieve this and when I write alert(reason)
the message I get is undefined
.... I need the content of the textarea for a php script...
What's wrong here???
Upvotes: 0
Views: 57
Reputation: 15052
When you already have an id
assigned to your text area, why don't you directly use that?
var reason = $("#reason_3").val();
Or something like this :
$("#reason_"+option).val();
Upvotes: 1
Reputation: 82251
You need :
$("input[value='" + option + "']").siblings(".reason").find("textarea").val()
or
$("input[value='" + option + "']").siblings(".reason textarea").val()
Upvotes: 1
Reputation: 388436
you need to use find() instead of has() - also instead of next(), you need to use siblings()
var option = 3;
var reason = $("input[value='" + option + "']").siblings(".reason").find("textarea").val();
Demo: Fiddle
.reason
element is not the next sibling of the input element, it is the next of next element, so either you can use .next().next('.reason')
or use siblings()
.has()
is used to filter the element set against an descendant selector, not to find an descendant element - so use .find()
instead.Upvotes: 5