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?

5
  • 3
    Is your code sample correct? Looks like it should say $('#txtArea1').val('new value');. Commented Oct 5, 2011 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. Commented Oct 5, 2011 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. Commented Oct 5, 2011 at 13:54
  • 1
    Use .html() instead of .val() to change the textarea contents. Commented Oct 5, 2011 at 13:54
  • 1
    This may be a browser specific issure; are you using Firefox? If so se this: stackoverflow.com/questions/1388893/… Commented Oct 5, 2011 at 14:02

6 Answers 6

2
$("#txtArea1").html("new value");
alert($("#txtArea1").html());
Sign up to request clarification or add additional context in comments.

1 Comment

It was a browser specific issue in the end. But your answer did help. Thanks
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.

Comments

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.

Comments

1

try this:

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

make that your last line.

Comments

1

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

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

to

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

Comments

1

Change this line:

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

To:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.