3

I am receiving data from state.jsp by JSON and displaying data in auto.jsp in a textbox having id textbox2.But i am not able to edit that textbox where i am receiving data, why?

//auto.jsp:

 $("#combo1").change(function() {
     // by onchange event of combobox, i am displaying string "anyname"
     // on that below textbox.
     $.getJSON('state.jsp', { combo1Val : $(this).val() }, function(responsedata) {
         $("#textbox2").replaceWith(responsedata.name);
     });
 });
 // i am displaying "anyname" here, but why i am not able
 // to edit this text box after displaying? I have not set it to readonly
 <input type="text" id="textbox2" name="2ndtextbox/> 

//state.jsp

<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%
JSONObject arrayObj= new JSONObject();
       arrayObj.put("name","anyname");// displaying "anyname" in that textbox
      response.setContentType("application/json");
      response.getWriter().write(arrayObj.toString());
%>

I am displaying string "anyname" in that text box but i am not able to edit this textbox anymore why? I have not set it to readonly. Any help

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
harry
  • 731
  • 10
  • 23

2 Answers2

2

.replaceWith() replaces the matched set by the value specified (text, a dom element, a jquery object). So in your code, you are replacing the while INPUT element with your response data instead of setting its value

To set the value of a form element, use the .val() method:

$("#textbox2").val(responsedata.name);
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
  • Glad it helped. You can accept the anwser by clicking the check-mark next to this answer then. – Didier Ghys Feb 09 '12 at 10:04
  • ya i am accepting it but it is telling to wait for 4 minutes more, can you please upvote my question – harry Feb 09 '12 at 10:06
  • please have a look at this question: http://stackoverflow.com/questions/9238267/clean-old-options-from-child-combo-box-when-receiving-data-by-json-array-objects – harry Feb 11 '12 at 10:01
2

you should do

  $("#textbox2").val(responsedata.name);

otherwise with replaceWith() you are replacing the DOM element with your text,that's why it's readonly

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • please have a look at this :http://stackoverflow.com/questions/9238267/clean-old-options-from-child-combo-box-when-receiving-data-by-json-array-objects – harry Feb 11 '12 at 10:02
  • please [see this question](http://stackoverflow.com/questions/9248383/retrieve-more-than-one-value-by-json-array-objects) Nicola, if possible please suggest some ideas – harry Feb 13 '12 at 04:01