Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[CS3 JS] Replace text in a paragraph

Community Expert ,
Jan 20, 2009 Jan 20, 2009
I want to replace all of the text in a paragraph, without removing the \r at the end of the paragraph. What is the best method for doing that? Thank you very much.

Rick Quatro
TOPICS
Scripting
557
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 20, 2009 Jan 20, 2009
Hi Rick,

Use itemByRange():

//Where myParagraph is a paragraph...

myParagraph.texts.itemByRange(myParagraph.characters.item(0), myParagraph.characters.item(-2)).remove();

Thanks,

Ole
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2009 Jan 20, 2009
Or maybe just
>myParagraph.contents = '\r';

Peter
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 20, 2009 Jan 20, 2009
Hi Peter,

The trouble with that approach, though, is that you run the risk of changing the style applied to the paragraph.

Thanks,

Ole
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2009 Jan 20, 2009
Thanks to Ole and Peter. Yes, I need to maintain the style that is applied to the paragraph. Also, I added this test in case the paragraph is empty already.

if (myParagraph.characters.length > 1) {
...
}

Rick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2009 Jan 20, 2009
LATEST
>The trouble with that approach, though, ...

Aha... hadn't realised that. In fact, it turns out that it changes the paragraph style applied to the next paragraph to that of paragraph whose contents is deleted. Didn't know that.

Peter
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines