coder0007 coder0007 - 2 months ago 6
Ajax Question

Display text in textarea using javascript

I have used the following code in index.jsp to set the value of textarea INPUT_TEXT and INPUT_TEXT2 in a form after extensive processing, wherein the text goes through a servlet, a java class and the preprocessed data then gets returned to the 2nd textarea.

CODE I:

<td>
<%
String txtvalue="";
if(request.getAttribute("x")!=null){
txtvalue=request.getAttribute("x").toString();
System.out.println("txtvalue= "+txtvalue);
}%>

<textarea id="INPUT_TEXT" name="INPUT_TEXT" style="font-size: 13pt;" rows="15" cols="50"><%=txtvalue%></textarea>
</td>

<td>
<%
String txt="";
if(request.getAttribute("y")!=null){
txt=request.getAttribute("y").toString();
System.out.println("txt= "+txt);
}%>

<textarea id="INPUT_TEXT2" name="INPUT_TEXT2" style="font-size: 13pt;" rows="15" cols="50"><%=txt%></textarea>
</td>


I have tried using

1)innerHTML,

2)setting the value using document.getElementById("INPUT_TEXT") and

3)the present method which I have given in the code I.

Unfortunately, nothing works!

Method 3 (code I) used to work, but then I had to make some changes in the function that is called after onclick, to make a POST request, which now looks like this:

CODE II

function submitPreprocessForm() {
var postData=$("#maryWebClient").serializeArray();
$.ajax({
url: "preprocess",
context:this,
type:"POST",
dataType:"JSON",
data:postData,
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success: function(response){
}
});
}


Now, the txt and txtvalue (Code I) are being printed only to the NetBeans output console (correctly, I might add) but not to the webpage, which gets reset when I click the submit button. Thus, although the entire internal functioning works perfectly, the only problem is text not being displayed in the respective textareas.
Kindly help.

Answer

The following approach solved my problem and text is displayed in the textrea.

success: function(response){
             $("#INPUT_TEXT").val(response["x"]);
             $("#INPUT_TEXT2").val(response["y"]);
         },

I had to remove the <%=txtvalue%> as it caused the textarea to be initialized to null.