Expand my Community achievements bar.

SOLVED

Editing text field that supports rich text using script

Avatar

Former Community Member

I have a text field with rich text format. I need to add a new line character if the length of a sentence exceeds 65 characters. When I edit the text value using javascript the formating breaks and the signature event fails eventually. Is there a way to edit the body of the text field value? I realized that the value is store in text/html.

below is sample:

Before edit:

<txtOtherDocuments><body xmlns="http://www.w3.org/1999/xhtml" xfa:APIVersion="Acroform:2.7.0.0" xfa:spec="2.1">

     <p style="margin-top:0pt;margin-bottom:0pt;font-family:Arial;font-size:8 pt;text-decoration:none">Test Data</p></body>

Using field.rawValue="...." fails. Is there a different set of commands?

Please help.

Thanks

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

If you want to access the data and its styling in a richtext field you have to use the command Fieldname.value.exData.saveXML("pretty"). This will give you the xHTML format of the data. Then you can manipulate the data the way you wish and to get it back into the field you have to use the Fieldname.value.exData.loadXML("xml to load", parm1, parm2). The parms are boolean values that will control whether to replace or append the xml to the existing xml. Refer to the Help/Scripting reference for a more detailed description.

Note that this will not be easy if there is lots of rich Text in your field as the text will be spread out between tags. You may have to use the rawValue (no formatting to know where to put the CR then find it in the HTML and add it there.

Make sense?

Paul

View solution in original post

8 Replies

Avatar

Correct answer by
Former Community Member

If you want to access the data and its styling in a richtext field you have to use the command Fieldname.value.exData.saveXML("pretty"). This will give you the xHTML format of the data. Then you can manipulate the data the way you wish and to get it back into the field you have to use the Fieldname.value.exData.loadXML("xml to load", parm1, parm2). The parms are boolean values that will control whether to replace or append the xml to the existing xml. Refer to the Help/Scripting reference for a more detailed description.

Note that this will not be easy if there is lots of rich Text in your field as the text will be spread out between tags. You may have to use the rawValue (no formatting to know where to put the CR then find it in the HTML and add it there.

Make sense?

Paul

Avatar

Former Community Member

Thanks Paul. Having the field set to accept Rich Text, doesn't allow control of font in a consistent manner. I switched back to Plain Text.

Could you please tell me how to add a new line in a Plain Text field? I did an export of the form and found that &#xD; is used as the new line character.

When I add a new line character, the text appears as is (dave&#xD;rita, instead of dave in one line and rita in the next line). I guess the characters are not recognized.

Here's what I'm doing:

var str = field1.rawValue;

//logic to check if the length is more that 65 characters. This result is an array with strings less than 65 characters

//finally I join the strings

field1.rawValue = lines.join("&#xD;");

I tried to escape characters and such but no luck.

Thanks,

RT

Avatar

Former Community Member

Hi Paul,

  However, the following doesn't work. I'm not able to split based on the new line character. Please help.

var value = field1.rawValue;

var lines = value.split("\n");

Thanks,

Ram

Avatar

Level 10

Hi Ram,

I think you want the carriage return character.

Try;

var lines = value.split("\r");

which is the same as;

var lines = value.split("\x0D");

Bruce

Avatar

Former Community Member

Works fine for me ......I did not use the var name value .....that is a reserved word .....try changing it to Fvalue instead and see if it works for you.

Paul

Avatar

Level 10

Hi Paul,

Both the new line and carriage return will cause the text to be displayed on multiple lines in Designer.

But it is only the carriage return which is escaped as &#xD; in the XML.

The new line (with a character code of 10) does not need to be escaped in the XML and in the xml view I use I get the value broken over two lines.

Regards

Bruce

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----