-1

I have a web service that brings back data and inserts it into the text area, i dont want this as a placeholder. the first time the user loads the page it should show the placeholder but once the webservice has run it should then show what has come back from the webservice. it has been working perfectly for me on chrome for months but now testing on IE and its not working. the text gets loaded but then disappears as the user clicks in the box. both the placeholder and the text gets removed. when looking at the source the data is being loaded in correctly with the text area showing:

<textarea rows="20" cols="50" placeholder="Insert things here" id="mytextarea">MY DATA</textarea>

in basic "MY DATA" disappears when i focus on the box in IE

Adam Bristow
  • 330
  • 2
  • 14
  • And how do you assign the new text to that element? Because your HTML offers no clues as to what's going wrong with your JavaScript/jQuery. – David Thomas Mar 05 '14 at 12:18
  • in basic $('#mytextarea').text("MY DATA"); – Adam Bristow Mar 05 '14 at 12:21
  • 2
    And if that was all there was, that would likely work. As it doesn't work, can you show what *else* is going on? We need to see the [Minimum, Complete, Valid Example](http://stackoverflow.com/help/mcve) in order to see what's going wrong (note that 'minimum' is essential), sometimes this'll let you work out for yourself where the problem is, but, either way, it makes it easier for us to offer valid help. Though note: "*`.text()` method cannot be used on `form` inputs...To set or get the text value of...`textarea` elements, use the `.val()` method.*" ([source](http://api.jquery.com/text/).) – David Thomas Mar 05 '14 at 12:24

2 Answers2

1

Changing

$('#mytextarea').text("MY DATA");

to

$('#mytextarea').val("MY DATA");

made it work because. textarea is element that takes some input, all these type of element(file, text, textarea, select, etc) has .val() method to fetch/store data, because they are not supposed to store anything inside(as child elements).

In other side elements that are not for interaction(get input from user) like div or section or table etc are for showing any section or data so that has .text() or .html() method. Both method here get/set child of the element (.val() does not do that).

.text() get/set as plain text while .html() does same but it treats data as html. Refer.

Community
  • 1
  • 1
Akshay
  • 3,558
  • 4
  • 43
  • 77
0

Changing:

$('#mytextarea').text("MY DATA");

to:

$('#mytextarea').val("MY DATA");

Made it work, not sure why or how that makes a difference thanks for the help even though I managed to figure it out myself.

Ex-iT
  • 1,479
  • 2
  • 12
  • 20
Adam Bristow
  • 330
  • 2
  • 14