There the problem : I need to generate html code from the contain of a given div. Then, I have to put this raw html code in a textarea. When I use the function $(textarea).val() like this :
$(textarea).val("some html like < input type='text' value='' style="background: url('http://www.w.com/bg.gif') repeat-x center;" /> bla bla");
or
$('#idTxtArGenHtml').val( $('idDivMain').html() );
I had problem with some special character ( & ' " ) when they are between quot. But when I use the function : $(textarea).html() the text is ok.
There an example form :
<FORM id="idFormContact" name="nFormContact" action="send.php" method="post" > <FIELDSET id="idFieldContact" class="CMainFieldset"> <LEGEND>Test your newsletter» </LEGEND> <p>Send to à : <input id='idInpMailList' type='text' value='[email protected]' /></p> <FIELDSET class="CChildFieldset"> <LEGEND>Subject</LEGEND> <LABEL for="idNomClient" class="CInfoLabel">Enter the subject: * </LABEL><BR/> <INPUT value="" name="nSubject" type="text" id="idSubject" class="CFormInput" alt="Enter the Subject" ><BR/> </FIELDSET> <FIELDSET class="CChildFieldset"> <INPUT id="idBtnGen" type="button" value="Generate" onclick="onGenHtml();"/> <INPUT id="idBtnSend" type="button" value="Send" onclick="onSend();"/><BR/><BR/> <LEGEND>Message</LEGEND> <LABEL for="idTxtArGenHtml" class="CInfoLabel">Html code : * </LABEL><BR/> <span><TEXTAREA name="nTxtArGenHtml" id="idTxtArGenHtml" width='100%' cols="69" rows="300" alt="enter your message" ></TEXTAREA></span> </FIELDSET> </FIELDSET> </FORM>
And javascript/jquery code that don't work to fill the textarea is :
function onGenHtml(){ $('#idTxtArGenHtml').html( $("#idDivMain").html() ); }
Finaly the solution :
function onGenHtml(){ $('#idTxtArGenHtml').html( $("#idDivMain").html() ); $('#idTxtArGenHtml').parent().replaceWith( '<span>'+$('#idTxtArGenHtml').parent().html()+'</span>'); }
The trick is wrap your textarea with a span tag to help with the replaceWith function. I'm not sure if it's very clean, but it's work perfect too add raw html code in a textarea.