0

I have a method that returns some text I would like to place all the text inside a textbox, how would I do that? This is what my function looks like:

function MethodName() {
  $.ajax({
     type: "POST",
     url: "@Url.Action("Method", "Controller")",
     data: JSON.stringify(),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (msg) {
     }
  });
}

This is where I would like to place:

<textarea id="someText"></textarea>

I have a menu e.g.

<div class="bs-example">
<ul class="list-group">
    <li id = 'testbx1' class="list-group-item active">text</li>
    <li id = 'testbx2' class="list-group-item">Documents</li>        
    <li id = 'testbx3' class="list-group-item">Music</li>
    <li id = 'testbx4' class="list-group-item">Videos</li>
</ul>

On click id testbx1 id ='someText' will appear and inside id='someText' data from database will be displayed. I have strongly typed model

Andy
  • 61,948
  • 13
  • 68
  • 95
  • 1
    `$('#someText').val(YourText)` to set value – Satpal Jun 23 '14 at 12:24
  • If your server is returning text, you don't want to specify `dataType: 'json'` in your AJAX options. jQuery is going to expect the server response to be JSON in that case. – Anthony Grist Jun 23 '14 at 12:25

3 Answers3

1

Here's how you would put text into a textarea:

document.getElementById("someText").value = msg;

Or with jQuery:

$('#someText').val(msg);

Update

If you're insisting on the complete code, try this:

function MethodName() {
  $.ajax({
     type: "POST",
     url: "@Url.Action("Method", "Controller")",
     data: JSON.stringify(),
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (msg) {
       $('#someText').val( JSON.stringify(msg) );
     }
  });
}
shennan
  • 10,798
  • 5
  • 44
  • 79
  • Not sure why I've been downvoted, this is the only correct answer as of writing. – shennan Jun 23 '14 at 12:27
  • 1
    +1 for you. your solutions address the problem, don't know why the down vote too :/. – João Pinho Jun 23 '14 at 12:29
  • @shennan your method works but it returns everything in my respond body. The data are not clear, what if I just want to get column1 I tried `$('#someText').val( JSON.stringify(column1) );` but it gives me `{}` empty results? –  Jun 23 '14 at 13:12
  • @User911 I don't know what data you're returning so I can't be sure, but you could try `$('#someText').val( JSON.stringify(msg.column1) );`. Be mindful that your list of *wants* is growing as time goes on. Accept an appropriate answer and create another SO question for any further issues you have. – shennan Jun 23 '14 at 13:33
0

On your success callback just use this code:

 success: function (msg) {
     $("#someText").val(msg);
 }
João Pinho
  • 3,725
  • 1
  • 19
  • 29
-1
document.getElementById("someText").value=msg;

or by jquery

$("#someText").val(msg);
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
  • do i place the above code in success handler? –  Jun 23 '14 at 12:25
  • I am using `$("#someText").html(msg);` in my respond body I am getting the data but it doesnt show inside textarea? –  Jun 23 '14 at 12:30
  • @User911 sorry, i updated my answer please use `.val(msg);` not `.html(msg);` – Govind Singh Jun 23 '14 at 12:31
  • Inside the box it shows [object Object]. What does that means –  Jun 23 '14 at 12:33
  • @User911 means the data comes from server is json object – Govind Singh Jun 23 '14 at 12:35
  • 1
    You can try `$("#someText").val(msg[0]);` this one – Jitendra Yadav Jun 23 '14 at 12:36
  • http://stackoverflow.com/questions/24365857/how-would-i-place-data-inside-a-textbox#comment37677933_24365857 – Govind Singh Jun 23 '14 at 12:37
  • @Govind Singh Nagarkoti its not working I am still getting `[object Object]` inside the textbox –  Jun 23 '14 at 12:39
  • @GovindSinghNagarkoti I did but I am yet getting Object object message in my `Response body` I have all the data. I try removing ` data: JSON.stringify(),` but it still gives me object object –  Jun 23 '14 at 12:43
  • @User911 can you call `alert(JSON.stringify(msg));` and tell me its response inside the success – Govind Singh Jun 23 '14 at 12:44
  • @Govind Singh Nagarkoti It says `[{"Id":0"Column1":"Column1","Column2":"Column2" etc....}]` do i have to specify `msg` anywhere? –  Jun 23 '14 at 12:47
  • @User911 if you want to print `Column1` use `.val(msg["Column1"]);` and same for any other – Govind Singh Jun 23 '14 at 12:49
  • @User911 if you want as it is complete json then `.val(JSON.stringify(msg));` – Govind Singh Jun 23 '14 at 12:54
  • Ok I tries column1 I get another error `[object HTMLTextAreaElement]` I am returning as json? in my controller I am mentioning `return Json(staff, "text/json", JsonRequestBehavior.AllowGet);` –  Jun 23 '14 at 12:58
  • There was a problem with textbox if i use table it works. –  Jun 23 '14 at 13:34