0

I will be straight to the point. I created a textarea element and I have to return the tag html plus the text inside.

var elemField = $('<textarea></textarea>');
elemField.val('bla bla this is my textarea text');
//changing some properties
elemField.attr("disabled", "disabled");
elemField.width('95%');    

found this solution:

var elemFeldCode = elemField.clone().wrap('<p>').parent().html().toString();

Output:

"<textarea disabled="disabled" style="width: 95%;"></textarea>"

Desired Output:

"<textarea disabled="disabled" style="width: 95%;">bla bla this is my textarea text</textarea>"

Any one can help me to have this result in a cross browser manner throw jquery?!

eKelvin
  • 921
  • 1
  • 9
  • 25
  • Use val method ... Check this >[Stackoverflowthread](http://stackoverflow.com/questions/144810/jquery-get-textarea-text) – Stphane Feb 25 '13 at 16:42
  • @f00bar pls read the post well I don't want the textarea text, look the desired output. – eKelvin Feb 25 '13 at 17:00

2 Answers2

1

For your desired output you'll have to use .text() to set the content of the text area as .val() does not affect the html of the node.

elemField.text('bla bla this is my textarea text');

DEMO

Musa
  • 96,336
  • 17
  • 118
  • 137
0

You can add it explicitly:

var elemFeldCode = elemField.clone().wrap('<p>').parent().html()
    .replace('</',elemField.val()+'</').toString();
Blazemonger
  • 90,923
  • 26
  • 142
  • 180