-2

So, I got the page on my local server, and this page contains textarea and button. I am Trying to write onclick function to button, which would read whatever I typed in text area, and make record to database. Just like when I finish typing my question here, and press Ask Question. The problem is that I can't properly read text in text area, it basically sees only what was in there at the moment of loading a page, and just rewrite it. How should I get text, that I typed right before clicking the button? I just want to know how can I copy that text to some var, which I can PUT to database.

$.getJSON('/link/' + tenderId, function (data) { 
     var description = ''; 
     description += '<textarea id="description" class="form-control" rows="3">' + data.description + '</textarea>'; 
     $('#description').html(description); 
});
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202

4 Answers4

0

You need to use val() (I am guessing you are using text()) to get the text of a Text Area:

$('input#mybutton').click(function() {
    var text = $('textarea#mytextarea').val();
});

Of course this is just guesswork as you did not supply any code! :)

Update:

The code you added is also incorrect as it adds a duplicate id of description inside a div with an id of description! ID's need to be unique on a page.

Assuming you want a new id here is a cleaner version of your code:

$.getJSON('/link/' + tenderId, function (data) {
    var $textArea = $("<textarea>", {class: "form-control", id: "descriptionText", rows: "3"}).val(data.description);
    $('#description').empty().append(description);
});
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
0

Use this code inside your click event

var textareaValue = $('textarea#textareaId').val();

html

<textarea id="textareaId"></textarea>

SO answer: https://stackoverflow.com/a/144836/2772017

Community
  • 1
  • 1
intCoffee
  • 184
  • 2
  • 4
0
$('button').click(function(){
var myString = $('#description').val();
})

Then use myString whereever you like.

Litestone
  • 529
  • 7
  • 22
0

I can't figure out you method logic; it seems you are pulling some json data then appending it to a textarea while in issue description you said that you are trying to save the textarea content so you have to be sending it throug a POST request.

Also does the <textarea id="description"...> element is there in your page or you will be creating it at each button click? If such is you case, you can try with the following snippet:

$.getJSON('/link/' + tenderId, function (data) { 
  var $description = $("<textarea>");
  $description.attr({
      id:'description',
      class:'form-control',
      rows:'3'})
    .html(data.description);
//you will have then to append this jQuery element, e.g: $("#wrapper").append($description)
});
tmarwen
  • 15,750
  • 5
  • 43
  • 62