MaxNagler
MaxNagler

Reputation: 69

Content of textarea is not being delivered

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

Answers (3)

Kazekage Gaara
Kazekage Gaara

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

Milind Anantwar
Milind Anantwar

Reputation: 82251

You need :

$("input[value='" + option + "']").siblings(".reason").find("textarea").val()

or

$("input[value='" + option + "']").siblings(".reason textarea").val()

Working Fiddle

Upvotes: 1

Arun P Johny
Arun P Johny

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

  • The .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

Related Questions