4

I'm trying to change the text inside the textarea with id="code" but $("#code").val(newtext) doesn't work.. What could be the problem? I'm using CodeMirror for highlighting text inside, if this matters Thanks

Ram
  • 143,282
  • 16
  • 168
  • 197
Andrei Ivanov
  • 651
  • 1
  • 9
  • 23
  • That looks good to me so can you show us on jsfiddle.net? Do you get an error? What's in `newtext`? – Explosion Pills Jan 26 '13 at 08:10
  • You need to show more of your code, something that shows the problem. `$('#code').val(newtext)` works fine: http://jsfiddle.net/FYkVT/ – JJJ Jan 26 '13 at 08:10

2 Answers2

2

textarea does not have the value attribute; therefore, it better for you to use .text() function

$("#id").text(newValue);

Update: Well, I had this problem once in my old project, but after changing to .text(), then it works (got solution from here). I know that .val could be applied, too, but if you get trouble like this (it could be because of your browser compatibility, version of jquery...) and you are sure that your selector code is correct, then choose either way (val or text)

Community
  • 1
  • 1
Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • 1
    *"The .val() method is primarily used to get the values of form elements such as input, select and **textarea**."* (http://api.jquery.com/val/) – JJJ Jan 26 '13 at 08:16
  • this still doesn't work.. I went reading deeper and the problem is CodeMirror which generates the textarea. I tried many solutions I found, but none of them work for me.. Please help! – Andrei Ivanov Jan 26 '13 at 17:41
  • do you have $(document).ready(function() { //set value to textarea )}; on your page? – Thai Tran Jan 26 '13 at 18:32
0
encodeURIComponent($("#code").val(newtext))
Amir
  • 4,089
  • 4
  • 16
  • 28