function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SS KarthickSS Karthick 

Rich text area field to text area field

Hi Everybody,
         Can anyone give me the code for coping the rich text area content to Text area field after save. The text area should not contain any html tags in it


Please Help!

Best Answer chosen by SS Karthick
Vikash TiwaryVikash Tiwary
Hello Karthick,

Rich ext area field can also contain images. when we have images in rich text area field we can not copy it to the textarea field because <textarea> element can only hold plain text, not other nodes. If you just want to copy the plain text you can do as such:
<textarea><apex:outputtext value="{!objName.richTextAreaFieldName}" escape = "false"/></textarea> 

Here escape = "false" attribute on outputtext renders the content escaping HTML tags.

If you want to do it in javascript: 

<apex:outputtext id="richVal" value="{!objName.richTextAreaFieldName}" escape = "false"/>
<textarea id="someId" rows="4" cols="50"></textarea>

<script>
document.getElementById('someId').value = document.getElementById({!$Component.richVal}).value;
</script>

Please let me know if it helps.

Thanks.

All Answers

James LoghryJames Loghry
In Apex you could do something as simple as

   myObj.TextField__c = myObj.RichTextField__c;  
Or you could write a workflow field update to copy the Rich Text field into the text area field.

If you want to control how many characters come back into the Text Area, in the workflow field update you could use the "LEFT" function, or if you want to control the number of characters in Apex, you can use the substring method.

  More on the LEFT formula function here: https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm

More on the Apex String substr method here: http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm (https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm)
Vikash TiwaryVikash Tiwary
Hello Karthick,

Rich ext area field can also contain images. when we have images in rich text area field we can not copy it to the textarea field because <textarea> element can only hold plain text, not other nodes. If you just want to copy the plain text you can do as such:
<textarea><apex:outputtext value="{!objName.richTextAreaFieldName}" escape = "false"/></textarea> 

Here escape = "false" attribute on outputtext renders the content escaping HTML tags.

If you want to do it in javascript: 

<apex:outputtext id="richVal" value="{!objName.richTextAreaFieldName}" escape = "false"/>
<textarea id="someId" rows="4" cols="50"></textarea>

<script>
document.getElementById('someId').value = document.getElementById({!$Component.richVal}).value;
</script>

Please let me know if it helps.

Thanks.
This was selected as the best answer
Vikash TiwaryVikash Tiwary
Hi Karhick,

You can copy the rich text area field to text area field and then replace the HTML tags with blank. Alike example is given below
 
News__c obj = [select Story__c from News__c where Id = 'a2Nc0000000DL3H'];

system.debug('------'+obj.Story__c); //RichtextArea

obj.Specials_Friday__c = obj.Story__c; //Specials_Friday__c //textarea field

while(obj.Specials_Friday__c.indexOf('<') > -1 && obj.Specials_Friday__c.indexOf('>') > -1)
{
string strtobeReplaced = obj.Specials_Friday__c.substring(obj.Specials_Friday__c.indexOf('<'), obj.Specials_Friday__c.indexOf('>'));
  
obj.Specials_Friday__c = obj.Specials_Friday__c.replace(strtobeReplaced, ' ');
 }

system.debug('----------------obj.Specials_Friday__c--'+obj.Specials_Friday__c);

I have not tested this but you may try after some modification.

Let me know if that helps!

Thanks.
SS KarthickSS Karthick
Hy Vikash Tiwary,
The following lines are not working 
string strtobeReplaced = obj.Specials_Friday__c.substring(obj.Specials_Friday__c.indexOf('<'), obj.Specials_Friday__c.indexOf('>'));
 
obj.Specials_Friday__c = obj.Specials_Friday__c.replace(strtobeReplaced, ' ');
      Can you fix that 
Vikash TiwaryVikash Tiwary
Hi Karthick,

I have tested this out after some modification and now it works perfectly.

Intranet_News__c obj = [select Story__c from Intranet_News__c where Id = 'a2Nc0000000DL3H'];
system.debug('------'+obj.Story__c);
obj.Specials_Friday__c = obj.Story__c;
while(obj.Specials_Friday__c.indexOf('<') > -1 && obj.Specials_Friday__c.indexOf('>') > -1)
{
   Integer i =  obj.Specials_Friday__c.indexOf('<');
   Integer j = obj.Specials_Friday__c.indexOf('>');
  string strtobeReplaced = obj.Specials_Friday__c.substring(i, j+1);
  system.debug('------------str--'+strtobeReplaced);   
  obj.Specials_Friday__c = obj.Specials_Friday__c.replace(strtobeReplaced, '');
    system.debug('------------str--'+obj.Specials_Friday__c);
}
system.debug('----obj.Specials_Friday__c-'+obj.Specials_Friday__c);

Please mark this as an answer if it works so that other in the community may be benefitted.

Thanks.