1

If i have a Textarea inside div. Something like

   <div id="section3">
    <textarea id="txtArea1">old value
    </textarea>
   </div>

And by using:

alert($('#section3').html());
$('#txtArea1').val('new value');
alert($('#section3').html());

both alerts show the same markup with ("old value"); is there any way i can flush the html so the last statement gets the updated html markup?

mircea .
  • 241
  • 5
  • 15
  • 3
    Is your code sample correct? Looks like it should say `$('#txtArea1').val('new value');`. – robyaw Oct 05 '11 at 13:47
  • sorry about the wrong id. But that's not my issue. I just wrote it wrong. That's not actualy my code it's a simplified version. But even with txtArea1 i have the same html() result. – mircea . Oct 05 '11 at 13:51
  • At first, I thought you were having problems with the last alert showing html tags instead of just text. Now I'm just confused. – rgin Oct 05 '11 at 13:54
  • 1
    Use `.html()` instead of `.val()` to change the textarea contents. – Shadow The GPT Wizard Oct 05 '11 at 13:54
  • 1
    This may be a browser specific issure; are you using Firefox? If so se this: http://stackoverflow.com/questions/1388893/jquery-html-in-firefox-uses-innerhtml-ignores-dom-changes – Andy Rose Oct 05 '11 at 14:02

6 Answers6

2
$("#txtArea1").html("new value");
alert($("#txtArea1").html());
James Johnson
  • 45,496
  • 8
  • 73
  • 110
1

You need to use .val() with textareas and jQuery. It's slightly counter-intuitive seeing as the textarea content is inside the tags, as opposed to being a value="", but it's how it's done by jQuery to keep consistency.

alert($('#section3').val());

$('#textarea').val('new value');

alert($('#section3').val());

Will give you the new value.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
1

try this:

alert( $('section3').text() );

make that your last line.

rgin
  • 2,291
  • 4
  • 24
  • 32
1

You do not have textarea with id "textarea". try to change

$('#textarea').val('new value');

to

$('#txtArea1').val('new value');
genesis
  • 50,477
  • 20
  • 96
  • 125
1

Change this line:

 $('#txtArea1').val('new value');

To:

 $('#txtArea1').text('new value');
robyaw
  • 2,274
  • 2
  • 22
  • 29
1

I think this is probably a browser specific issue. You can test it out using this jsFiddle.
The above code fails in Firefox but works in IE. See this stackoverflow question and answer for more detail.

Community
  • 1
  • 1
Andy Rose
  • 16,770
  • 7
  • 43
  • 49