AndreaNobili
AndreaNobili

Reputation: 42957

Why I can't retrieve the text inserted into a textarea tag using jQuery?

I am pretty new in JavaScript and jQuery and I have the following problem.

Into a page I have the following textarea having id="notaCorrente":

<div class="form-group">
    <label for="comment">Inserire una nota:</label>
    <textarea id="notaCorrente" class="form-control" rows="5"></textarea>
</div> 

in which the user can insert some text.

I don't want to submit this textarea into a form (there is not a form) but I want to retrieve the inserted value using jQuery (because then I have to perform an AJAX call using the retrieved text instead the post submit).

So I have do in this way:

function rimettiInLavorazioneProgetto() {
    alert("INTO rimettiInLavorazioneProgetto()");
    
    var testoNotaCorrente = $('#notaCorrente').text();
    
    alert("NOTA CORRENTE: " + testoNotaCorrente);
}

As you can see I try to retrieve the text inside the object having id="notaCorrente" but it can't work. I enter into the method but when print the second alert the retrieved text is always empty also if I have inserted some text into my textarea.

Why? What am I missing?

Upvotes: 0

Views: 131

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

Use val() not text() , because it will not return correct value sometimes. For more visit val() vs. text() for textarea

function rimettiInLavorazioneProgetto() {
    alert("INTO rimettiInLavorazioneProgetto()");    
    var testoNotaCorrente = $('#notaCorrente').val();    
    alert("NOTA CORRENTE: " + testoNotaCorrente);
}

Upvotes: 2

Related Questions