-1

I have some contend in textarea ..When I alert it give correct contend .But when I append in "hello" text in text contend area .and get contend on button click it does not give updated contend why ? it should show with "hello" contend.It show previous contend why ?

here is code https://jsfiddle.net/4oa1q9qe/1/

$(document).ready(function(){
 alert($("#test").html())   


 $("#btn").click(function(){
      alert($("#test").html())   
 })


}) 
  • Add "hello" in text area and get get the contend it gives same contend why as previous ?
user944513
  • 12,247
  • 49
  • 168
  • 318
  • Please don't link to code, everything needed to understand the problem and desired result should be contained in the question. – firelynx Jul 07 '15 at 12:25

4 Answers4

3

Use val() option to get the text value of input or textarea elements

The .val() method is primarily used to get the values of form elements such as input, select and textarea

$(document).ready(function(){
 alert($("#test").val())  

 $("#btn").click(function(){
      alert($("#test").val())   
 })
})

DEMO

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
2

use $("#test").val() instead of $("#test").html()

$(document).ready(function(){
 alert($("#test").val())   
 
 
 $("#btn").click(function(){
      alert($("#test").val())   
 })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea rows="4" cols="50" id="test">
At w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies. 
</textarea>
<button id="btn">btn</button>
ozil
  • 6,930
  • 9
  • 33
  • 56
1

It show previous contend why ?

The inner content of the <textarea> element, e.g. foo in

<textarea>foo</textarea>

is used as the default value. Changing the value of the textarea does not change its inner content, it only changes the value property. Hence when you use .html(), you get the value that is present in the original HTML.

As already mentioned, use .val() to get the current value. See also val() vs. text() for textarea .

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

use .val() instead of .html()

.val() returns or sets the value of a <textarea> or <input>. It has no effect on anything else.

$("button").click(function() {
  alert($("textarea").val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea></textarea>
<button>Show data</button>
Zesty Citrus
  • 68
  • 1
  • 9